package com.scalar.chiptrack.sales.actions.quote;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.operations.packingslip.PackingSlipComparator;
import com.scalar.chiptrack.alarms.Alarm;
import com.scalar.chiptrack.alarms.AlarmUser;
import com.scalar.chiptrack.commons.*;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.sales.*;
import com.scalar.chiptrack.sales.actions.SalesOrderAction;
import com.scalar.chiptrack.sales.dao.*;
import com.scalar.chiptrack.sales.dao.quote.QuotationDAO;
import com.scalar.chiptrack.sales.forms.quote.QuotationForm;
import com.scalar.chiptrack.sales.forms.quote.ApproverQuotationForm;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.dao.UserDAO;
import com.scalar.chiptrack.utils.ConfigManager;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.utils.FileUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
import org.apache.struts.upload.FormFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;
import java.io.File;

/**
 * Implementation of <strong>Action</strong> that gives customers list,
 * search with customer name, create Quotation
 *
 * @author Basha
 * @version $Revision: 1.35 $ $Date: 2004/02/04
 */

public final class ChangeQuotationAction extends ChipTrackAction
{

    private static final String ACTION_NAME = "/changeQuotation.do"; // TBD
    int isSample = 1;

    public ActionForward execute ( ActionMapping mapping,
                                   ActionForm form,
                                   HttpServletRequest request,
                                   HttpServletResponse response )
            throws Exception
    {

        QuotationForm subform = ( QuotationForm ) form;
        HttpSession session = request.getSession ();

        User user = ( User ) session.getAttribute ( SessionKeys.USER_KEY );
        String action = request.getParameter ( "action" );

        if ( user == null )
        {
            if( "printView".equals ( action ) )
            {
                request.setAttribute ( "closeWindowAfterSessionExp", "true" );
			    return ( mapping.findForward( "printView" ) );
            }
            else{
                session.setAttribute ( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString () );
                return ( mapping.findForward ( "login" ) );
            }
        }
        HashMap errors = new HashMap ();
        HashMap info = new HashMap ();

        String customerId = request.getParameter ( "customerId" );
        Quotation quotationFromMap = null;
        HashMap  quotationMap = ( HashMap ) session.getAttribute( "quotationMap" );

        if ( customerId != null )
        {
            if ( quotationMap != null )
            {
                quotationFromMap = ( Quotation ) quotationMap.get( customerId );
                if( quotationFromMap == null )
                {
                    if ( action != null ){
                        errors.put( ERROR_CHANGE_QUOTATION_ALREADY_SAVED_FOR_SAME_CUSTOMER, null );
                        request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
                        action = null;
                    }
                    else
                    {
                        quotationFromMap = new Quotation();
                        quotationMap.put( customerId, quotationFromMap );
                    }
                }
             }
        }
        if ( "UpdateQuotation".equals ( action ) )
        {
            addNewSalesTaxState ( request, session, errors, subform );

            if ( ( subform.getShippingAddressId () == -1 ) && ( session.getAttribute ( "newShippingAddressId" ) != null ) )
            {
                subform.setShippingAddressId ( Integer.parseInt ( ( String ) session.getAttribute ( "newShippingAddressId" ) ) );
                Address address = CustomerDAO.getAddress( subform.getShippingAddressId() );
                loadShippingAddress( address, quotationFromMap.getShippingAddresses(), quotationFromMap );
            }
            subform.setShippingMethod ( subform.getShippingAccount () );
            session.setAttribute ( "QuotationForm", subform );
            ArrayList lineItems = loadLineItems ( request, subform.getCustomerId (), errors, info );
            request.setAttribute ( "lineItems", lineItems );
            request.setAttribute ( "lineItemsWithSubtotals", SalesOrderAction.loadLineItemsWithSubTotals ( lineItems ) );
            session.setAttribute ( "maxLineItemNo", request.getAttribute ( "maxLineItemNo" ) );
            subform.setSalesTaxAsDouble(Double.parseDouble((String)request.getAttribute("salesTax")) );
            subform.setSubTotalAsDouble(Double.parseDouble((String)request.getAttribute("subTotal")) );
            subform.setOrderTotalAsDouble(subform.getSubTotalAsDouble()+subform.getFrieghtAsDouble()+subform.getMiscAsDouble()+subform.getSalesTaxAsDouble());

            request.setAttribute ( "quoteTypes", QuotationDAO.getQuoteTypeLabelValueList( null ) );
            request.setAttribute ( "applications", QuotationDAO.getQuoteApplicationLabelValueList( null ) );
            request.setAttribute ( "screenDisplayFiledsList", QuotationDAO.getScreenDisplayFiledsList( null ) );
            request.setAttribute ( "Account", subform.getShippingAccount () );
            request.setAttribute ( "Method", subform.getShippingAccount () );
            if ( subform.getOdmId() > 0 )
            {
                quotationFromMap.setSalesPersons( NewCustomerDAO.loadAllSalesPersonsforCustomerAndOdm( subform.getCustomerId(), subform.getOdmId() ) ) ;
            }
            else
            {
                quotationFromMap.setSalesPersons( CustomerDAO.loadAllSalesPersonsforCustomer( subform.getCustomerId ())) ;
            }
            quotationFromMap.setShippingMethods( CustomerDAO.loadShippingMethodsForCustomerAndOdm( subform.getCustomerId(), subform.getOdmId() ));
            request.setAttribute ( "QuotationForm", subform );
            errors = ( HashMap ) request.getAttribute ( "Errors" );

            if ( !errors.isEmpty () )
            {
                request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
            }

            saveToken ( request );

            return ( mapping.findForward ( "Quotation" ) );
        }
        else if ( "SubmitQuotation".equals ( action ) )
        {

            if ( ( subform.getShippingAddressId () == -1 ) && ( session.getAttribute ( "newShippingAddressId" ) != null ) )
            {
                subform.setShippingAddressId ( Integer.parseInt ( ( String ) session.getAttribute ( "newShippingAddressId" ) ) );
                Address address = CustomerDAO.getAddress( subform.getShippingAddressId() );
                loadShippingAddress( address, quotationFromMap.getShippingAddresses(), quotationFromMap );
                session.removeAttribute ( "newShippingAddressId" );
            }
            addNewShippingMethod ( request, session, errors, info, subform, quotationFromMap );
            addNewSalesTaxState ( request, session, errors, subform );
            QuotationAction.addNewDeliveryTerms( request, session, errors, info, subform );
            QuotationAction.addNewShippingTerms ( request, session, errors, info, subform );
            QuotationAction.addNewShippingInstructions ( request, session, errors, info, subform );
            QuotationAction.addNewFreightInstructions( request, session, errors, info, subform );
            session.setAttribute ( "QuotationForm", subform );
            if ( subform.getOdmId() > 0 )
            {
                quotationFromMap.setSalesPersons( NewCustomerDAO.loadAllSalesPersonsforCustomerAndOdm( subform.getCustomerId(), subform.getOdmId() ) ) ;
            }
            else
            {
                quotationFromMap.setSalesPersons( CustomerDAO.loadAllSalesPersonsforCustomer( subform.getCustomerId ())) ;
            }
            ArrayList lineItems = loadLineItems ( request, subform.getCustomerId (), errors, info );
            session.setAttribute ( "lineItems", lineItems );
            session.setAttribute ( "lineItemsWithSubtotals", SalesOrderAction.loadLineItemsWithSubTotals ( lineItems ) );
            session.setAttribute ( "maxLineItemNo", request.getAttribute ( "maxLineItemNo" ) );
            request.setAttribute ( "Account", subform.getShippingAccount () );
            request.setAttribute ( "Method", subform.getShippingAccount () );
            request.setAttribute ( "quoteTypes", QuotationDAO.getQuoteTypeLabelValueList( null ) );
            request.setAttribute ( "applications", QuotationDAO.getQuoteApplicationLabelValueList( null ) );
            request.setAttribute ( "screenDisplayFiledsList", QuotationDAO.getScreenDisplayFiledsList( null ) );

            subform.setTotalQty ( Integer.parseInt ( ( String ) request.getAttribute ( "totalQty" ) ) );
            subform.setSalesTaxAsDouble(Double.parseDouble((String)request.getAttribute("salesTax")) );
            subform.setSubTotalAsDouble(Double.parseDouble((String)request.getAttribute("subTotal")) );
            subform.setOrderTotalAsDouble(subform.getSubTotalAsDouble()+subform.getFrieghtAsDouble()+subform.getMiscAsDouble()+subform.getSalesTaxAsDouble());

            session.setAttribute ( "totalQty", request.getAttribute ( "totalQty" ) );
            session.setAttribute ( "salesTax", request.getAttribute ( "salesTax" ) );
            session.setAttribute ( "subTotal", request.getAttribute ( "subTotal" ) );
            session.setAttribute ( "orderTotal", Double.toString ( subform.getSalesTaxAsDouble () + subform.getFrieghtAsDouble () + subform.getMiscAsDouble () + subform.getSubTotalAsDouble () ) );
            session.setAttribute ( "tempShippingMethod", subform.getShippingMethod () );
            subform.setOdm( CustomerDAO.getCustomerName(null,subform.getOdmId()) );
            subform.setEndCustomer( CustomerDAO.getCustomerName( null, subform.getEndCustomerId() ) );

            request.setAttribute ( "QuotationForm", subform );
            errors = ( HashMap ) request.getAttribute ( "Errors" );
            if ( !errors.isEmpty () )
            {
                request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
                return ( mapping.findForward ( "Quotation" ) );
            }

            info = ( HashMap ) request.getAttribute ( "Info" );
            if ( !info.isEmpty () )
            {
                request.setAttribute ( SessionKeys.INFO_MESSAGES, info );
            }

            saveToken ( request );

            return ( mapping.findForward ( "ConfirmQuotation" ) );
        }
        else if ( "printView".equals ( action ) )
        {
            ApproverQuotationForm approverQuotationForm = new ApproverQuotationForm ();
            Quotation quotation = new Quotation ();
            QuotationForm quotationForm = ( QuotationForm ) session.getAttribute ( "QuotationForm" );
            Customer customer = new Customer();
            customer.setCustomerId( quotationForm.getCustomerId() );
            CustomerDAO.getCustomerDetailsForAcknowledgement( null, customer );
            request.setAttribute ( "customer", customer );

            PropertyUtils.copyProperties ( quotation, quotationForm );
            QuotationDAO.loadClientAddress ( quotation );
            quotation.setShippingAddressAsObject( CustomerDAO.getAddressWithContactPersonDetails( quotation.getShippingAddressId(), quotation .getShippingContactPerson()) );
            quotation.setBillingAddressAsObject( CustomerDAO.getAddressWithContactPersonDetails( quotation.getBillingAddressId(), quotation.getBillingContactPerson(), quotation.getCustomerId() ) );

            quotation.setShippingAccount ( ( String ) session.getAttribute ( "tempShippingMethod" ) );

            quotation.setTotalQty ( ( String ) session.getAttribute ( "totalQty" ) );
            quotation.setFinalSubTotal ( Double.parseDouble ( ( String ) session.getAttribute ( "subTotal" ) ) );
            quotation.setFinalSalesTax ( Double.parseDouble ( ( String ) session.getAttribute ( "salesTax" ) ) );
            quotation.setFinalOrderTotal ( Double.parseDouble ( ( String ) session.getAttribute ( "orderTotal" ) ) );
            quotation.setNewEndUserName(quotationForm.getNewEndUserName() );
            quotation.setNewEndCustomerName (quotationForm.getNewEndCustomerName() );
            approverQuotationForm.setQuotationId( quotationForm.getQuotationId() );
            approverQuotationForm.setQuotationNo( quotationForm.getQuotationNo() );
            String clientaddressId =  UserDAO.getClientAddressId();
            String addressId = null;
            String clientname = null;

            int finalAddressId = 0;
            if(clientaddressId != null)
            {
                StringTokenizer st = new StringTokenizer(clientaddressId,"@");
                try
                {
                    addressId = st.nextToken();
                    clientname = st.nextToken();
                }catch(NoSuchElementException e)
                {
                     clientname  = "";
                }
            }
            if(addressId  != null)
            {
                finalAddressId = Integer.parseInt(addressId);
            }
            Address clientAddress= UserDAO.loadClientAddressDetails( null , finalAddressId );
            quotation.setClientAddressAsObject(clientAddress);
            String clientAddress1 = SalesOrder.getClientAddressAsString(clientAddress);
            quotation.setClientAddress(clientAddress1);
            quotation.setClientName(clientname);

            approverQuotationForm.setQuotation( quotation );

            request.setAttribute ( "approverQuotationForm", approverQuotationForm );
            request.setAttribute ( "lineItems", session.getAttribute ( "lineItems" ) );

            ArrayList list = (ArrayList) request.getAttribute("lineItems");
            if ( list != null )
            {
                Collections.sort (list , new PackingSlipComparator () );
            }
            request.setAttribute ( "termsAndConditions", SalesDAO.loadTermsAndConditionsAsList () );

             request.setAttribute ( "lineItemsWithSubtotals", SalesOrderAction.loadLineItemsWithSubTotals( list ) );
             return ( mapping.findForward ( "printView" ) );
        }
        else if ( "EditQuotation".equals ( action ) )
        {
            if ( !isTokenValid ( request ) )
            {
                errors.put ( ERROR_QUOTATION_ALREADY_SAVED, null );
            }

            resetToken ( request );

            if ( !errors.isEmpty () )
            {
                request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
                saveToken ( request );
                return ( mapping.findForward ( "successQuotation" ) );
            }

            request.setAttribute ( "QuotationForm", session.getAttribute ( "QuotationForm" ) );
            subform.setShippingMethod ( subform.getShippingAccount () );
            String hdnShippingMethodName = request.getParameter ( "hdnShippingMethodName" );
            String hdnshippingAccount = request.getParameter ( "hdnshippingAccount" );

            request.setAttribute ( "quoteTypes", QuotationDAO.getQuoteTypeLabelValueList( null ) );
            request.setAttribute ( "applications", QuotationDAO.getQuoteApplicationLabelValueList( null ) );
            request.setAttribute ( "screenDisplayFiledsList", QuotationDAO.getScreenDisplayFiledsList( null ) );
            request.setAttribute ( "Account", hdnshippingAccount );
            request.setAttribute ( "Method", hdnshippingAccount );
            request.setAttribute ( "lineItems", request.getAttribute ( "lineItems" ) );
            session.setAttribute ( "maxLineItemNo", session.getAttribute ( "maxLineItemNo" ) );
            quotationFromMap.setShippingMethods( CustomerDAO.loadShippingMethodsForCustomerAndOdm( subform.getCustomerId(), subform.getOdmId() ));
            saveToken ( request );

            return ( mapping.findForward ( "Quotation" ) );
        }
        else if ( "ConfirmQuotation".equals ( action ) )
        {
            if ( !isTokenValid ( request ) )
            {
                errors.put ( ERROR_QUOTATION_ALREADY_SAVED, null );
            }

            resetToken ( request );

            if ( !errors.isEmpty () )
            {
                request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
                saveToken ( request );
                return ( mapping.findForward ( "successQuotation" ) );
            }
            Quotation quotation = new Quotation ();
            PropertyUtils.copyProperties ( quotation, session.getAttribute ( "QuotationForm" ) );
            String method = "";
            String account = "";

            String temp = quotation.getShippingAccount ();
            account = temp;
            if ( temp != null )
            {
                int index1 = temp.indexOf ( "#" );

                if ( index1 != -1 && index1 != 0 )
                {
                    method = temp.substring ( 0, index1 ).trim ();
                    account = temp.substring ( index1 + 1, temp.length () ).trim ();
                }
            }
            quotation.setShippingAccount ( account );

            String methodName = quotation.getShippingMethodName ();
            int index = methodName.indexOf ( "#" );
            if ( index != -1 && index != 0 )
            {
                quotation.setShippingMethodName ( methodName.substring ( 0, index ).trim () );
            }

            DBTransaction dbTransaction = null;
            boolean success = false;
            int salesOrderId = -1;
            Alarm alarm = null;
            String lineItemChangesSummary = null;
            try
            {
                dbTransaction = DBTransaction.getInstance ();
                dbTransaction.begin ();
                quotation.setIsSample ( ( isSample == 0 ) ? true : false );

                quotation.setEnteredById ( user.getUserId () );
                quotation.setEnteredByName ( user.getFirstName () + " " + user.getLastName () );

                salesOrderId = QuotationDAO.saveQuotation ( dbTransaction, quotation );

                lineItemChangesSummary = QuotationDAO.getNotificationChangeSummary ( dbTransaction, quotation.getQuotationId(), ( ArrayList ) session.getAttribute ( "lineItems" ) );

                QuotationDAO.saveChangeLineItems ( dbTransaction, salesOrderId, ( ArrayList ) session.getAttribute ( "lineItems" ), user.getUserId () );
                if( ConfigManager.isLoadQuotationQuestionnaire() )
                {
                    FormFile formFile =( FormFile ) session.getAttribute( "questionnairefile" + quotation.getQuotationNo() );
                    String fileLocation = ConfigManager.getProcessDocFilePath();
                    fileLocation += "/quotationQuestionnaire" + "/" + quotation.getQuotationNo() + "/";

                    session.removeAttribute( "questionnairefile" + quotation.getQuotationNo() );
                    File outputDir = new File( fileLocation );
                    if ( outputDir.exists() == false )
                    {
                        outputDir.mkdirs();
                    }
                    if ( quotation.isContractPriceFlag()  )
                    {
                        if ( formFile != null && formFile.getFileSize() > 0)
                        {
                            AdaptorUtils.removeOldFiles( fileLocation, null );
                            FileUtils.saveFile ( formFile.getFileData(), fileLocation + formFile.getFileName() );
                        }
                    }
                    else
                    {
                        AdaptorUtils.removeOldFiles( fileLocation, null );
                    }
                    quotation.setContractPriceFileName ( fileLocation + formFile.getFileName() );
                    QuotationDAO.saveQuotationQuestionnaire ( dbTransaction, quotation );
                }
                String reason = quotation.getReason ();
                alarm = QuotationDAO.updateAlarmForChangeQuotation(dbTransaction, reason, salesOrderId, user);
                success = true;
                request.setAttribute ("quotationId", Integer.toString ( salesOrderId));
                request.setAttribute ("quotationNo", quotation.getQuotationNo() );
            }
            catch ( ScalarException se )
            {
                errors.put ( ERROR_INTERNAL_SERVER_ERROR, ERROR_IN_SAVING_QUOTATION_DETAILS );
                errors.put ( ERROR_MESSAGE_CONTACT_SYS_ADMN, null );
                session.setAttribute ( SessionKeys.ERROR_REPORT_SUBJECT, " Error: Could Not Save Quote Details " );
                session.setAttribute ( SessionKeys.ERROR_REPORT, se );
            }
            finally
            {
                if ( success == true )
                {
                    dbTransaction.commit ();

                    try
                    {
                        String quoteChangeSummary = getQuotationChangeSummary ( null, QuotationDAO.loadQuotation ( String.valueOf ( salesOrderId ), false ), ( Quotation ) session.getAttribute ( "oldQuotation" ) );
                        //alarm.setRecipients ( AlarmDAO.loadRecipientsWithChangeQuoteAccess ( alarm ) );
                        if ( quotation.getSalesPerson() != null && quotation.getSalesPerson().length() > 0 )
                        {
                            SalesPerson salesPerson = NewCustomerDAO.getSalesPersonDetails( null, quotation.getSalesPerson() );
                            if ( salesPerson != null && salesPerson.isQuoteAckMail() )
                            {
                                if ( salesPerson.getAddress() != null && salesPerson.getAddress().getEmail() != null && salesPerson.getAddress().getEmail().trim().length() > 0 )
                                {
                                    alarm.addRecipient ( new AlarmUser( salesPerson.getSalesPerson (), salesPerson.getAddress().getEmail().trim() ) );
                                }
                            }
                        }

                        String quotationStatus = QuotationDAO.getQuotationStatus( quotation.getQuotationNo(), "0", "0", "1", user.getUserId(), "", "0", "", "" );

                        alarm.setSubject( "Quote Notification - Quote Number: " + quotation.getQuotationNo() + " - Customer: " + quotation.getCustomerName() + " - Status: " + quotationStatus );
                        
                        alarm.notifyAllRecipients ( quotation.getHtmlViewWithChangeSummary ( quotation.getQuotationId(), quoteChangeSummary+lineItemChangesSummary ) );
                        errors.put ( "Email ", " has been sent to all approvers" );
                    }
                    catch ( Exception e )
                    {
                        errors.put ( ERROR_INTERNAL_SERVER_ERROR, ERROR_IN_SENDING_MAILS_TO_APPROVERS );
                        errors.put ( ERROR_MESSAGE_CONTACT_SYS_ADMN, null );
                        session.setAttribute ( SessionKeys.ERROR_REPORT_SUBJECT, " Error: Could Not Send Mail to all Approvers " );
                        session.setAttribute ( SessionKeys.ERROR_REPORT, e );
                    }

                    if ( isSample == 1 )
                    {
                        errors.put ( "Quote = " + quotation.getQuotationNo (), "Saved Successfully" );
                        //errors.put ( "Email ", " has been sent to all approvers" );
                        errors.put ( "View/Print ", "Click here to <a href='javascript:openQuotation();'onmouseover=\"window.status='Click here to View/print Quatation';return true\" onmouseout=\"window.status='';return true\"> view/print </a> Quote Details" );
                    }
                    else
                    {
                        errors.put ( "SampleOrder = " + quotation.getQuotationNo (), "Saved Successfully" );
                        //errors.put ( "Email ", " has been sent to all approvers" );
                        errors.put ( "View/Print ", "Click here to <a href='javascript:openQuotation();'onmouseover=\"window.status='Click here to View/print Quatation';return true\" onmouseout=\"window.status='';return true\" > view/print </a>Sample Order Details" );
                    }
                }
                else
                {
                    dbTransaction.rollback ();

                    if ( isSample == 1 )
                    {
                        errors.put ( ERROR_INTERNAL_SERVER_ERROR, ERROR_IN_SAVING_QUOTATION_DETAILS );
                        errors.put ( ERROR_MESSAGE_CONTACT_SYS_ADMN, null );
                        session.setAttribute ( SessionKeys.ERROR_REPORT_SUBJECT, " Error: Could Not Save Quote Details " );
                        session.setAttribute ( SessionKeys.ERROR_REPORT, null );
                    }
                    else
                    {
                        errors.put ( ERROR_INTERNAL_SERVER_ERROR, ERROR_IN_SAVING_QUOTATION_DETAILS );
                        errors.put ( ERROR_MESSAGE_CONTACT_SYS_ADMN, null );
                        session.setAttribute ( SessionKeys.ERROR_REPORT_SUBJECT, " Error: Could Not Save Quote Details " );
                        session.setAttribute ( SessionKeys.ERROR_REPORT, null );
                    }
                }
            }

            if ( !errors.isEmpty () )
            {
                request.setAttribute ( SessionKeys.MESSAGES, errors );
            }
            session.removeAttribute ( "lineItems" );
            session.removeAttribute ( "lineItemsWithSubtotals" );
            session.removeAttribute ( "maxLineItemNo" );
            session.removeAttribute ( "totalQty" );
            session.removeAttribute ( "salesTax" );
            session.removeAttribute ( "subTotal" );
            session.removeAttribute ( "orderTotal" );
            session.removeAttribute ( "tempShippingMethod" );
            session.removeAttribute ( "allShippingTerms" );
            session.removeAttribute ( "allDeliveryTerms" );
            quotationMap.remove( quotation.getCustomerId()+"" );
        }
        else if ( "loadEndUserDetails".equals ( action ) )
        {
            String endUserId = request.getParameter ( "endUserId" );
            int quotationId = Integer.parseInt(request.getParameter ( "quotationId" ));
            ArrayList lineItems = null;
            if ( ( subform.getShippingAddressId () == -1 ) && ( session.getAttribute ( "newShippingAddressId" ) != null ) )
                subform.setShippingAddressId ( Integer.parseInt ( ( String ) session.getAttribute ( "newShippingAddressId" ) ) );
            if ( endUserId != null )
            {
                if ( "".equals ( endUserId.trim () ) )
                {
                    Quotation quotation = QuotationDAO.getQuotationCustomerDetails ( Integer.toString ( subform.getCustomerId () ) );
                    subform.setContactPersons ( quotation.getContactPersons () );
                    subform.setContactPersonsForBillingAddress ( quotation.getContactPersonsForBillingAddress () );
                    subform.setContactPersonsForShippingAddress ( quotation.getContactPersonsForShippingAddress () );
                    subform.setBillingAddress ( SalesOrder.getAddressAsString ( quotation.getBillingAddressAsObject () ));
                    subform.setShippingAddress ( SalesOrder.getAddressAsString ( quotation.getShippingAddressAsObject () ));

                    HashMap billingAddressMap = CustomerDAO.getAddressDetailsMap ( quotation.getBillingAddressId() , quotation.getBillingAddressAsObject ());
                    Address quotationAddress = QuotationDAO.getQuotationAddress ( null,quotationId );
                    Address address= CustomerDAO.getAddress ( quotation.getBillingAddressId () );
                    if( quotationAddress != null )
                    {
                        address.setPhoneNumber(quotationAddress.getPhoneNumber());
                        address.setCellNumber("");
                        address.setFaxNumber(quotationAddress.getFaxNumber());
                        address.setEmail(quotationAddress.getEmail());
                    }
                    address.setContactPerson(subform.getCustomerName());
                    address.setCellNumber("");
                    billingAddressMap.put("",SalesOrder.getAddressAsString(address));
                    session.setAttribute ( "billingAddressMap", billingAddressMap  );
                    HashMap shippingAddressMap = CustomerDAO.getAddressDetailsMap ( quotation.getShippingAddressId() , quotation.getShippingAddressAsObject ());
                    Address shipAddress = CustomerDAO.getAddress ( quotation.getShippingAddressId () );
                    shipAddress.setContactPerson("");
                    shipAddress.setPhoneNumber("");
                    shipAddress.setCellNumber("");
                    shipAddress.setFaxNumber("");
                    shipAddress.setEmail("");
                    shippingAddressMap.put("",SalesOrder.getAddressAsString(shipAddress));
                    session.setAttribute ( "shippingAddressMap", shippingAddressMap );
                    subform.setShippingAddresses ( quotation.getShippingAddresses () );
                    subform.setEndUser ( "" );
                    subform.setBillingAddressId ( quotation.getBillingAddressId () );
                    subform.setShippingAddressId ( quotation.getShippingAddressId () );
                    subform.setBillingContactPerson ( quotation.getBillingContactPerson () );
                    subform.setShippingContactPerson ( quotation.getShippingContactPerson () );
                    session.setAttribute ( "mfgPartsWithCustomerPartNos", NewSalesOrderDAO.loadManfacturingPartNumbersWithCustomerPartNumbers ( subform.getCustomerId (), subform.getOdmId(), isSample ) );
                    session.setAttribute ( "mfgPartsWithLabelNumbers", NewSalesOrderDAO.loadManfacturingPartNumbersWithOdmLabelNumbers ( subform.getCustomerId (), subform.getOdmId(), isSample ) );
                }
                else
                {
                    EndUser endUser = EndUserDAO.getEndUserDetails ( Integer.parseInt ( endUserId ) );
                    subform.setContactPersonsForBillingAddress(CustomerDAO.getAllContactPersonsForAddress( endUser.getDefaultBillingAddressId() ));// Added by veeru on 26th April 2004
                    subform.setContactPersonsForShippingAddress(CustomerDAO.getAllContactPersonsForAddress( endUser.getDefaultShippingAddressId() ));// Added by veeru on 26th April 2004
                    subform.setEndUser ( endUser.getEndUserName () );

                    ArrayList shippingAddresses = endUser.getShippingLocations ();
                    shippingAddresses.add ( 0, endUser.getShippingAddress () );

                    subform.setOdmId( -1 );
                    subform.setOdm("");
                    subform.setBillingAddressId ( endUser.getDefaultBillingAddressId () );
                    subform.setShippingAddressId ( endUser.getDefaultShippingAddressId () );
                    subform.setBillingAddress ( SalesOrder.getAddressAsString ( endUser.getBillingAddress () ) );
                    subform.setShippingAddress ( SalesOrder.getAddressAsString ( endUser.getShippingAddress () ) );
                    Quotation quotation = QuotationDAO.getQuotationCustomerDetails ( Integer.toString ( subform.getCustomerId () ) );

                    subform.setBillingContactPerson(endUser.getBillingContactPersonObject().getContactPerson());
                    subform.setShippingContactPerson(endUser.getShippingContactPersonObject().getContactPerson());
                    subform.setContactPersons ( quotation.getContactPersons () );
                    HashMap billingAddressMap = CustomerDAO.getAddressDetailsMap (  subform.getBillingAddressId() , endUser.getBillingAddress ());

                    Address customerAddressObject = CustomerDAO.getAddress ( quotation.getBillingAddressId () );
                    Address quotationAddress = QuotationDAO.getQuotationAddress ( null,quotationId );
                    Address billAddress = CustomerDAO.getAddress ( endUser.getDefaultBillingAddressId () );
                    String billContactPersonName = billAddress.getContactPerson();
                    if( quotationAddress != null)
                    {
                        billAddress.setPhoneNumber(quotationAddress.getPhoneNumber());
                        billAddress.setFaxNumber(quotationAddress.getFaxNumber());
                        billAddress.setEmail(quotationAddress.getEmail());
                    }
                    else
                    {
                        billAddress.setPhoneNumber(customerAddressObject.getPhoneNumber());
                        billAddress.setFaxNumber(customerAddressObject.getFaxNumber());
                        billAddress.setEmail(customerAddressObject.getEmail());
                    }
                    billAddress.setContactPerson(subform.getCustomerName());
                    billAddress.setCellNumber("");

                    billingAddressMap.put("",SalesOrder.getAddressAsString(billAddress));
                    session.setAttribute ( "billingAddressMap", billingAddressMap  );
                    if( billContactPersonName == null || billContactPersonName.trim().length() == 0  || billContactPersonName.equals(subform.getCustomerName() ) )
                    {
                        subform.setBillingAddress(SalesOrder.getAddressAsString(billAddress));
                    }
                    HashMap shippingAddressMap = CustomerDAO.getAddressDetailsMap ( subform.getShippingAddressId() , endUser.getShippingAddress ());
                    Address shipAddress = CustomerDAO.getAddress(endUser.getDefaultShippingAddressId());
                    shipAddress.setContactPerson("");
                    shipAddress.setPhoneNumber("");
                    shipAddress.setCellNumber("");
                    shipAddress.setFaxNumber("");
                    shipAddress.setEmail("");
                    shippingAddressMap.put("",SalesOrder.getAddressAsString(shipAddress));
                    session.setAttribute ( "shippingAddressMap", shippingAddressMap  );
                    Address address = endUser.getBillingAddress();

                    String billAddressWithoutContactPerson = SalesOrderAction.getAddressAsString(address);
                    address = endUser.getShippingAddress();
                    String shipAddressWithoutContactPerson = SalesOrderAction.getAddressAsString(address);

                    if ( billAddressWithoutContactPerson.trim ().equals ( "" ) )
                    {
                        subform.setBillingAddressId ( quotation.getBillingAddressId () );
                        Address billingAddress = quotation.getBillingAddressAsObject ();
                        subform.setBillingAddress ( SalesOrder.getAddressAsString ( billingAddress ) );
                        subform.setContactPersonsForBillingAddress ( quotation.getContactPersonsForBillingAddress () );
                        subform.setBillingContactPerson ( quotation.getBillingContactPerson () );
                        billingAddress = CustomerDAO.getAddress ( quotation.getBillingAddressId () );
                        billingAddressMap = CustomerDAO.getAddressDetailsMap ( quotation.getBillingAddressId() , quotation.getBillingAddressAsObject () );
                        if( quotationAddress != null )
                        {
                            billingAddress.setPhoneNumber(quotationAddress.getPhoneNumber());
                            billingAddress.setFaxNumber(quotationAddress.getFaxNumber());
                            billingAddress.setEmail(quotationAddress.getEmail());
                        }
                        billingAddress.setContactPerson(subform.getCustomerName());
                        billingAddress.setCellNumber("");
                        billingAddressMap.put("",SalesOrder.getAddressAsString(billingAddress));
                        session.setAttribute ( "billingAddressMap", billingAddressMap  );
                    }
                    if(shipAddressWithoutContactPerson.trim().equals(""))
                    {
                        subform.setShippingAddressId ( quotation.getShippingAddressId () );
                        Address shippingAddress = quotation.getShippingAddressAsObject();

                        subform.setShippingAddress ( SalesOrder.getAddressAsString ( shippingAddress ) );
                        subform.setShippingAddressId( quotation.getShippingAddressId() );
                        //shippingAddresses.add( shippingAddress );
                        subform.setContactPersonsForShippingAddress ( quotation.getContactPersonsForShippingAddress () ); //Added by veeru on 25th April 2004
                        subform.setShippingContactPerson(quotation.getShippingContactPerson());
                        shippingAddressMap = CustomerDAO.getAddressDetailsMap ( quotation.getShippingAddressId() , quotation.getShippingAddressAsObject ());
                        shipAddress = CustomerDAO.getAddress ( quotation.getShippingAddressId () );
                        shipAddress.setContactPerson("");
                        shipAddress.setPhoneNumber("");
                        shipAddress.setCellNumber("");
                        shipAddress.setFaxNumber("");
                        shipAddress.setEmail("");
                        shippingAddressMap.put("",SalesOrder.getAddressAsString(shipAddress));
                        session.setAttribute ( "shippingAddressMap", shippingAddressMap );
                        subform.setShippingAddresses ( quotation.getShippingAddresses () );
                    }
                    //Added by sujan on 17th dec 2004 as to get the customer default shipping address when the end user is selected for the quotation.
                    shippingAddresses.add ( CustomerDAO.getAddress ( SalesOrderDAO.getDefaultShippingAddressId( subform.getCustomerId() ) ) );
                    //end adding by sujan.
                    subform.setShippingAddresses ( loadShippingAddresses ( shippingAddresses ) );

                    session.setAttribute ( "mfgPartsWithCustomerPartNos", EndUserDAO.loadManfacturingPartNumbersWithEndUserPartNos ( Integer.parseInt ( endUserId ) ) );
                    session.setAttribute ( "mfgPartsWithLabelNumbers", EndUserDAO.loadManfacturingPartNumbersWithEndUserLabelNumbers( Integer.parseInt ( endUserId ) ) );
                }
                subform.setIsEndCustomerShipAddress( false ); // Added by Veeru on 21st Dec 2004
            }
            session.setAttribute ( "contactPersons", subform.getContactPersons () );
            session.setAttribute ( "billingAddressContactPersons", subform.getContactPersonsForBillingAddress() );
            session.setAttribute ( "shippingAddressContactPersons", subform.getContactPersonsForShippingAddress() );

            lineItems = loadLineItems ( request, subform.getCustomerId (), errors, info );

            /*if ( "".equals ( endUserId ) )
            {
                SalesOrderDAO.loadCustomerPartNumbersForLineItems ( null, lineItems, subform.getCustomerId (), subform.getOdmId() );
            }
            else
            {
                SalesOrderDAO.loadEndUserPartNumbersForLineItems ( null, lineItems, Integer.parseInt ( endUserId ) );
            }*/

            request.setAttribute ( "lineItems", lineItems );

            request.setAttribute ( "lineItemsWithSubtotals", SalesOrderAction.loadLineItemsWithSubTotals ( lineItems ) );
            session.setAttribute ( "maxLineItemNo", request.getAttribute ( "maxLineItemNo" ) );

            subform.setSalesTaxAsDouble(Double.parseDouble((String)request.getAttribute("salesTax")) );
            subform.setSubTotalAsDouble(Double.parseDouble((String)request.getAttribute("subTotal")) );
            subform.setOrderTotalAsDouble(subform.getSubTotalAsDouble()+subform.getFrieghtAsDouble()+subform.getMiscAsDouble()+subform.getSalesTaxAsDouble());


            //session.setAttribute ( "shippingMethods", session.getAttribute ( "shippingMethods" ) );
            session.setAttribute ( "shippingMethods", CustomerDAO.loadSalesOrderShippingMethods ( subform.getCustomerId() )  );
            session.setAttribute ( "shippingAddresses", subform.getShippingAddresses () );
            session.setAttribute ( "salesPersons",CustomerDAO.loadAllSalesPersonsforCustomer(subform.getCustomerId () ) ) ;
            session.setAttribute ( "mktgParts", SalesOrderDAO.loadAllMarketingPartNumbers () );
            session.setAttribute ( "salesTaxes", SalesDAO.loadSalesTaxList () );
            session.setAttribute ( "mfgParts", SalesOrderDAO.loadAllManfacturingPartNosOfCustomer ( subform.getCustomerId (), isSample ) );
            request.setAttribute ( "Account", subform.getShippingAccount () );
            request.setAttribute ( "Method", subform.getShippingAccount () );
            request.setAttribute ( "quoteTypes", QuotationDAO.getQuoteTypeLabelValueList( null ) );
            request.setAttribute ( "applications", QuotationDAO.getQuoteApplicationLabelValueList( null ) );
            request.setAttribute ( "screenDisplayFiledsList", QuotationDAO.getScreenDisplayFiledsList( null ) );

            request.setAttribute ( "QuotationForm", subform );

            saveToken ( request );
            return ( mapping.findForward ( "Quotation" ) );
        }
        else if ( "loadODMDetails".equals ( action ) )
        {
            String odmId = request.getParameter ( "odmId" );
            int quotationId = Integer.parseInt(request.getParameter ( "quotationId" ));
            ArrayList lineItems = null;

            if ( ( subform.getShippingAddressId () == -1 ) && ( session.getAttribute ( "newShippingAddressId" ) != null ) )
            {
                subform.setShippingAddressId ( Integer.parseInt ( ( String ) session.getAttribute ( "newShippingAddressId" ) ) );
                Address address = CustomerDAO.getAddress( subform.getShippingAddressId() );
                loadShippingAddress( address, quotationFromMap.getShippingAddresses(), quotationFromMap );
            }

            if ( odmId != null )
            {
                if ( "-1".equals ( odmId.trim () ) )
                {
                    Quotation quotation = QuotationDAO.getQuotationCustomerDetails ( Integer.toString ( subform.getCustomerId () ) );
                    subform.setContactPersons ( quotation.getContactPersons () );
                    subform.setContactPersonsForBillingAddress ( quotation.getContactPersonsForBillingAddress () );
                    subform.setContactPersonsForShippingAddress ( quotation.getContactPersonsForShippingAddress () );
                    //subform.setBillingAddress ( SalesOrder.getAddressAsString ( quotation.getBillingAddressAsObject () ));
                    subform.setShippingAddress ( SalesOrder.getAddressAsString ( quotation.getShippingAddressAsObject () ));

                    //Commented by varma 0n 16 feb 2005 no need to load the billing address again from customer.
                    /*HashMap billingAddressMap = CustomerDAO.getAddressDetailsMap ( quotation.getBillingAddressId() , quotation.getBillingAddressAsObject ());
                    Address quotationAddress = QuotationDAO.getQuotationAddress ( null,quotationId );
                    Address address= CustomerDAO.getAddress ( quotation.getBillingAddressId () );
                    if( quotationAddress != null )
                    {
                        address.setPhoneNumber(quotationAddress.getPhoneNumber());
                        address.setCellNumber("");
                        address.setFaxNumber(quotationAddress.getFaxNumber());
                        address.setEmail(quotationAddress.getEmail());
                    }
                    address.setContactPerson(subform.getCustomerName());
                    address.setCellNumber("");
                    billingAddressMap.put("",SalesOrder.getAddressAsString(address));
                    session.setAttribute ( "billingAddressMap", billingAddressMap  );*/
                    HashMap shippingAddressMap = CustomerDAO.getAddressDetailsMap ( quotation.getShippingAddressId() , quotation.getShippingAddressAsObject ());
                    Address shipAddress = CustomerDAO.getAddress ( quotation.getShippingAddressId () );
                    shipAddress.setContactPerson("");
                    shipAddress.setPhoneNumber("");
                    shipAddress.setCellNumber("");
                    shipAddress.setFaxNumber("");
                    shipAddress.setEmail("");
                    shippingAddressMap.put("",SalesOrder.getAddressAsString(shipAddress));
                    quotationFromMap.setShippingAddressMap( shippingAddressMap );
                    //session.setAttribute ( "shippingAddressMap", shippingAddressMap );
                    subform.setShippingAddresses ( quotation.getShippingAddresses () );
                    quotationFromMap.setShippingAddresses( quotation.getShippingAddresses () );
                    subform.setOdm("");
                    //subform.setBillingAddressId ( quotation.getBillingAddressId () );
                    subform.setShippingAddressId ( quotation.getShippingAddressId () );
                    //subform.setBillingContactPerson ( quotation.getBillingContactPerson () );
                    subform.setShippingContactPerson ( quotation.getShippingContactPerson () );
                    quotationFromMap.setMfgPartsWithCustomerPartNos( NewSalesOrderDAO.loadManfacturingPartNumbersWithCustomerPartNumbers ( subform.getCustomerId (), subform.getOdmId(), isSample ) );
                    quotationFromMap.setMfgPartsWithLabelNumbers( NewSalesOrderDAO.loadManfacturingPartNumbersWithOdmLabelNumbers ( subform.getCustomerId (), subform.getOdmId(), isSample ) );
                    quotationFromMap.setSalesPersons( CustomerDAO.loadAllSalesPersonsforCustomer( subform.getCustomerId () ) );
                    //session.setAttribute ( "mfgPartsWithCustomerPartNos", NewSalesOrderDAO.loadManfacturingPartNumbersWithCustomerPartNumbers ( subform.getCustomerId (), subform.getOdmId() ) );
                    //session.setAttribute ( "mfgPartsWithLabelNumbers", NewSalesOrderDAO.loadManfacturingPartNumbersWithOdmLabelNumbers ( subform.getCustomerId (), subform.getOdmId() ) );
                    //session.setAttribute ( "salesPersons",CustomerDAO.loadAllSalesPersonsforCustomer( subform.getCustomerId () ) ) ;
                    subform.setShippingMethods( CustomerDAO.loadSalesOrderShippingMethods ( subform.getCustomerId() ) );
                }
                else
                {
                    Quotation quotation = QuotationDAO.getQuotationCustomerDetails ( Integer.toString ( subform.getCustomerId () ) );
                    Quotation odmQuotation = QuotationDAO.getQuotationCustomerDetails ( odmId );
                    Address customerShippingAddress = quotation.getShippingAddressAsObject();
                    subform.setOdm( CustomerDAO.getCustomerName( null, Integer.parseInt( odmId ) ) );

                    subform.setContactPersonsForBillingAddress( quotation.getContactPersonsForBillingAddress () );
                    subform.setContactPersonsForShippingAddress( odmQuotation.getContactPersonsForShippingAddress() );
                    //subform.setBillingAddress ( SalesOrder.getAddressAsString ( quotation.getBillingAddressAsObject () ) );
                    subform.setShippingAddress ( SalesOrder.getAddressAsString ( odmQuotation.getShippingAddressAsObject () ) );

                    //subform.setBillingContactPerson( quotation.getBillingContactPerson() );
                    subform.setShippingContactPerson( odmQuotation.getShippingContactPerson () );
                    //subform.setBillingAddressId ( quotation.getBillingAddressId () );
                    subform.setShippingAddressId ( odmQuotation.getShippingAddressId ()  );

                    ArrayList shippingAddresses = odmQuotation.getShippingAddresses ();

                    subform.setContactPersons ( quotation.getContactPersons () );
                    //Commented by varma 0n 16 feb 2005 no need to load the billing address again from customer.
                    /*HashMap billingAddressMap = CustomerDAO.getAddressDetailsMap (  quotation.getBillingAddressId() , quotation.getBillingAddressAsObject ());

                    Address customerAddressObject = CustomerDAO.getAddress ( quotation.getBillingAddressId () );
                    Address quotationAddress = QuotationDAO.getQuotationAddress ( null,quotationId );
                    Address billAddress = CustomerDAO.getAddress ( quotation.getBillingAddressId () );

                    String billContactPersonName = billAddress.getContactPerson();
                    if( quotationAddress != null)
                    {
                        billAddress.setPhoneNumber(quotationAddress.getPhoneNumber());
                        billAddress.setFaxNumber(quotationAddress.getFaxNumber());
                        billAddress.setEmail(quotationAddress.getEmail());
                    }
                    else
                    {
                        billAddress.setPhoneNumber(customerAddressObject.getPhoneNumber());
                        billAddress.setFaxNumber(customerAddressObject.getFaxNumber());
                        billAddress.setEmail(customerAddressObject.getEmail());
                    }
                    billAddress.setContactPerson(subform.getCustomerName());
                    billAddress.setCellNumber("");

                    billingAddressMap.put("",SalesOrder.getAddressAsString(billAddress));
                    session.setAttribute ( "billingAddressMap", billingAddressMap  );
                    if( billContactPersonName == null || billContactPersonName.trim().length() == 0  || billContactPersonName.equals(subform.getCustomerName() ) )
                    {
                        subform.setBillingAddress(SalesOrder.getAddressAsString(billAddress));
                    }*/

                    HashMap shippingAddressMap = CustomerDAO.getAddressDetailsMap ( odmQuotation.getShippingAddressId() , odmQuotation.getShippingAddressAsObject ());
                    Address shipAddress = CustomerDAO.getAddress(odmQuotation.getShippingAddressId());

                    shipAddress.setContactPerson("");
                    shipAddress.setPhoneNumber("");
                    shipAddress.setCellNumber("");
                    shipAddress.setFaxNumber("");
                    shipAddress.setEmail("");
                    shippingAddressMap.put("",SalesOrder.getAddressAsString(shipAddress));
                    //session.setAttribute ( "shippingAddressMap", shippingAddressMap  );
                    quotationFromMap.setShippingAddressMap( shippingAddressMap );
                    Address address = odmQuotation.getShippingAddressAsObject();
                    String shipAddressWithoutContactPerson = SalesOrderAction.getAddressAsString(address);

                    if(shipAddressWithoutContactPerson.trim().equals(""))
                    {
                        subform.setShippingAddressId ( quotation.getShippingAddressId () );
                        subform.setShippingAddress ( SalesOrder.getAddressAsString ( customerShippingAddress ) );
                        subform.setContactPersonsForShippingAddress ( quotation.getContactPersonsForShippingAddress () );
                        subform.setShippingContactPerson(quotation.getShippingContactPerson());
                        shippingAddressMap = CustomerDAO.getAddressDetailsMap ( quotation.getShippingAddressId() , quotation.getShippingAddressAsObject ());
                        shipAddress = CustomerDAO.getAddress ( quotation.getShippingAddressId () );
                        shipAddress.setContactPerson("");
                        shipAddress.setPhoneNumber("");
                        shipAddress.setCellNumber("");
                        shipAddress.setFaxNumber("");
                        shipAddress.setEmail("");
                        shippingAddressMap.put("",SalesOrder.getAddressAsString(shipAddress));
                        quotationFromMap.setShippingAddressMap( shippingAddressMap );
                        //session.setAttribute ( "shippingAddressMap", shippingAddressMap );
                    }
                    subform.setEndUser ( "" );
                    subform.setEndUserId ( "" );

                    ArrayList defaultShippingAddress = new ArrayList();
                    defaultShippingAddress.add( customerShippingAddress );
                    ArrayList farmatedDefualtShippingAddress = loadShippingAddresses ( defaultShippingAddress );
                    for ( int i = 0; i < farmatedDefualtShippingAddress.size(); i++ ) {
                        org.apache.struts.util.LabelValueBean bean = ( org.apache.struts.util.LabelValueBean ) farmatedDefualtShippingAddress.get( i );
                        shippingAddresses.add( bean );
                    }

                    subform.setShippingAddresses ( shippingAddresses );
                    quotationFromMap.setShippingAddresses( shippingAddresses );
                    //session.setAttribute ( "mfgPartsWithCustomerPartNos", NewSalesOrderDAO.loadManfacturingPartNumbersWithCustomerPartNumbers ( subform.getCustomerId (), subform.getOdmId() ) );
                    //session.setAttribute ( "mfgPartsWithLabelNumbers", NewSalesOrderDAO.loadManfacturingPartNumbersWithOdmLabelNumbers ( subform.getCustomerId (), subform.getOdmId() ) );
                    //session.setAttribute("salesPersons",CustomerDAO.loadAllSalesPersonsforCustomer( Integer.parseInt( odmId) ) ) ;
                    //session.setAttribute ( "salesPersons",NewCustomerDAO.loadAllSalesPersonsforCustomerAndOdm( subform.getCustomerId(), Integer.parseInt( odmId) ) ) ;
                    quotationFromMap.setMfgPartsWithCustomerPartNos( NewSalesOrderDAO.loadManfacturingPartNumbersWithCustomerPartNumbers ( subform.getCustomerId (), subform.getOdmId(), isSample ) );
                    quotationFromMap.setMfgPartsWithLabelNumbers( NewSalesOrderDAO.loadManfacturingPartNumbersWithOdmLabelNumbers ( subform.getCustomerId (), subform.getOdmId(), isSample ) );
                    quotationFromMap.setSalesPersons( NewCustomerDAO.loadAllSalesPersonsforCustomerAndOdm( subform.getCustomerId(), Integer.parseInt( odmId) ) );
                    subform.setShippingMethods( CustomerDAO.loadShippingMethodsForCustomerAndOdm ( subform.getCustomerId(), Integer.parseInt(odmId ) ) );
                    quotationFromMap.setShippingMethods( subform.getShippingMethods() );
                }
                subform.setIsEndCustomerShipAddress( true ); // Added by Veeru on 21st Dec 2004
            }
            quotationFromMap.setContactPersons( subform.getContactPersons () );
            quotationFromMap.setContactPersonsForBillingAddress( subform.getContactPersonsForBillingAddress() );
            quotationFromMap.setContactPersonsForShippingAddress( subform.getContactPersonsForShippingAddress() );
            //request.setAttribute ( "contactPersons", subform.getContactPersons () );
            //request.setAttribute ( "billingAddressContactPersons", subform.getContactPersonsForBillingAddress() );
            //request.setAttribute ( "shippingAddressContactPersons", subform.getContactPersonsForShippingAddress() );

            lineItems = loadLineItems ( request, subform.getCustomerId (), errors, info );

            /*if ( "-1".equals ( odmId ) )
            {
                SalesOrderDAO.loadCustomerPartNumbersForLineItems ( null, lineItems, subform.getCustomerId (), subform.getOdmId() );
            }
            else
            {
                SalesOrderDAO.loadCustomerPartNumbersForLineItems ( null, lineItems, subform.getCustomerId (), subform.getOdmId() );
                NewSalesOrderDAO.loadOdmLabelNumbersForLineItems ( null, lineItems, subform.getCustomerId (),Integer.parseInt ( odmId ) );
            }*/

            NewSalesOrderDAO.loadCustomerPartNumbersForQuoteLineItems ( null, lineItems, subform.getCustomerId (), subform.getOdmId() );
            request.setAttribute ( "lineItems", lineItems );

            request.setAttribute ( "lineItemsWithSubtotals", SalesOrderAction.loadLineItemsWithSubTotals ( lineItems ) );
            session.setAttribute ( "maxLineItemNo", request.getAttribute ( "maxLineItemNo" ) );

            subform.setSalesTaxAsDouble(Double.parseDouble((String)request.getAttribute("salesTax")) );
            subform.setSubTotalAsDouble(Double.parseDouble((String)request.getAttribute("subTotal")) );
            subform.setOrderTotalAsDouble(subform.getSubTotalAsDouble()+subform.getFrieghtAsDouble()+subform.getMiscAsDouble()+subform.getSalesTaxAsDouble());

            //session.setAttribute ( "shippingMethods", session.getAttribute ( "shippingMethods" ) );
            //session.setAttribute ( "shippingMethods", subform.getShippingMethods() );
            //request.setAttribute ( "shippingAddresses", subform.getShippingAddresses () );
            //session.setAttribute ( "mktgParts", SalesOrderDAO.loadAllMarketingPartNumbers () );
            //session.setAttribute ( "salesTaxes", SalesDAO.loadSalesTaxList () );
            //session.setAttribute ( "mfgParts", SalesOrderDAO.loadAllManfacturingPartNosOfCustomer ( subform.getCustomerId () ) );
            request.setAttribute ( "Account", subform.getShippingAccount () );
            request.setAttribute ( "Method", subform.getShippingAccount () );
            request.setAttribute ( "quoteTypes", QuotationDAO.getQuoteTypeLabelValueList( null ) );
            request.setAttribute ( "applications", QuotationDAO.getQuoteApplicationLabelValueList( null ) );
            request.setAttribute ( "screenDisplayFiledsList", QuotationDAO.getScreenDisplayFiledsList( null ) );

            request.setAttribute ( "QuotationForm", subform );

            saveToken ( request );
            return ( mapping.findForward ( "Quotation" ) );
        }
        else if ( "loadShippingAddress".equals ( action ) )
        {
            if ( ( subform.getShippingAddressId () == -1 ) && ( session.getAttribute ( "newShippingAddressId" ) != null ) )
            {
                subform.setShippingAddressId ( Integer.parseInt ( ( String ) session.getAttribute ( "newShippingAddressId" ) ) );
                Address address = CustomerDAO.getAddress( subform.getShippingAddressId() );
                loadShippingAddress( address, quotationFromMap.getShippingAddresses(), quotationFromMap );
            }

            int shippingAddressId = subform.getShippingAddressId();
            int defaultShippingAddressId = SalesOrderDAO.getDefaultShippingAddressId( subform.getCustomerId () );

            Address shipAddress = CustomerDAO.getAddressWithContactPersonDetails ( shippingAddressId );

            HashMap shippingAddressMap = new HashMap();
            if(shipAddress != null)
            {
                /*String shipAddressWithoutContactPerson = SalesOrderAction.getAddressAsString( shipAddress );
                if ( "".equals( shipAddressWithoutContactPerson.trim() ) )
                {
                    if ( defaultShippingAddressId != shippingAddressId )
                    {
                        shippingAddressId = defaultShippingAddressId;
                        subform.setShippingAddressId( shippingAddressId);
                        shipAddress = CustomerDAO.getAddressWithContactPersonDetails ( shippingAddressId );
                    }
                }*/
                subform.setShippingContactPerson( shipAddress.getContactPerson() );
                subform.setShippingAddress ( shipAddress.getAddressAsString ( ));
                shippingAddressMap = CustomerDAO.getAddressDetailsMap ( shippingAddressId, shipAddress);
                shippingAddressMap.put("",shipAddress.getAddressWithoutContactPersonAsString());
            }
            quotationFromMap.setShippingAddressMap( shippingAddressMap );
            quotationFromMap.setContactPersonsForShippingAddress( CustomerDAO.getAllContactPersonsForAddress(  shippingAddressId ) );
            ArrayList lineItems = null;

            lineItems = loadLineItems ( request, subform.getCustomerId (), errors, info );
            request.setAttribute ( "lineItems", lineItems );

            request.setAttribute ( "lineItemsWithSubtotals", SalesOrderAction.loadLineItemsWithSubTotals ( lineItems ) );
            session.setAttribute ( "maxLineItemNo", request.getAttribute ( "maxLineItemNo" ) );

            subform.setSalesTaxAsDouble(Double.parseDouble((String)request.getAttribute("salesTax")) );
            subform.setSubTotalAsDouble(Double.parseDouble((String)request.getAttribute("subTotal")) );
            subform.setOrderTotalAsDouble(subform.getSubTotalAsDouble()+subform.getFrieghtAsDouble()+subform.getMiscAsDouble()+subform.getSalesTaxAsDouble());
            session.setAttribute ( "allShippingTerms", session.getAttribute ( "allShippingTerms" ) );
            if ( subform.getOdmId() > 0 )
            {
                quotationFromMap.setSalesPersons( NewCustomerDAO.loadAllSalesPersonsforCustomerAndOdm( subform.getCustomerId(), subform.getOdmId() ) ) ;
            }
            else
            {
                quotationFromMap.setSalesPersons( CustomerDAO.loadAllSalesPersonsforCustomer( subform.getCustomerId ())) ;
            }
            quotationFromMap.setShippingMethods( CustomerDAO.loadShippingMethodsForCustomerAndOdm( subform.getCustomerId(), subform.getOdmId() ));
            request.setAttribute ( "Account", subform.getShippingAccount () );
            request.setAttribute ( "Method", subform.getShippingAccount () );
            request.setAttribute ( "quoteTypes", QuotationDAO.getQuoteTypeLabelValueList( null ) );
            request.setAttribute ( "applications", QuotationDAO.getQuoteApplicationLabelValueList( null ) );
            request.setAttribute ( "screenDisplayFiledsList", QuotationDAO.getScreenDisplayFiledsList( null ) );

            request.setAttribute ( "QuotationForm", subform );
            saveToken ( request );
            return ( mapping.findForward ( "Quotation" ) );
        } // End of loadShippingAddress

        else if ( "loadShippingInstructions".equals ( action ) )
        {
            if ( ( subform.getShippingAddressId () == -1 ) && ( session.getAttribute ( "newShippingAddressId" ) != null ) )
            {
                subform.setShippingAddressId ( Integer.parseInt ( ( String ) session.getAttribute ( "newShippingAddressId" ) ) );
                Address address = CustomerDAO.getAddress( subform.getShippingAddressId() );
                loadShippingAddress( address, quotationFromMap.getShippingAddresses(), quotationFromMap );
            }
            ArrayList lineItems = null;

            subform.setShippingInstructions ( NewSalesOrderDAO.loadSelectedShippingInstructions( null, subform.getShippingInstructionsId() ) );

            lineItems = loadLineItems ( request, subform.getCustomerId (), errors, info );
            request.setAttribute ( "lineItems", lineItems );

            request.setAttribute ( "lineItemsWithSubtotals", SalesOrderAction.loadLineItemsWithSubTotals ( lineItems ) );
            session.setAttribute ( "maxLineItemNo", request.getAttribute ( "maxLineItemNo" ) );

            subform.setSalesTaxAsDouble(Double.parseDouble((String)request.getAttribute("salesTax")) );
            subform.setSubTotalAsDouble(Double.parseDouble((String)request.getAttribute("subTotal")) );
            subform.setOrderTotalAsDouble(subform.getSubTotalAsDouble()+subform.getFrieghtAsDouble()+subform.getMiscAsDouble()+subform.getSalesTaxAsDouble());
            session.setAttribute ( "allShippingTerms", session.getAttribute ( "allShippingTerms" ) );
            if ( subform.getOdmId() > 0 )
            {
                quotationFromMap.setSalesPersons( NewCustomerDAO.loadAllSalesPersonsforCustomerAndOdm( subform.getCustomerId(), subform.getOdmId() ) ) ;
            }
            else
            {
                quotationFromMap.setSalesPersons( CustomerDAO.loadAllSalesPersonsforCustomer( subform.getCustomerId ())) ;
            }
            quotationFromMap.setShippingMethods( CustomerDAO.loadShippingMethodsForCustomerAndOdm( subform.getCustomerId(), subform.getOdmId() ));
            request.setAttribute ( "Account", subform.getShippingAccount () );
            request.setAttribute ( "Method", subform.getShippingAccount () );
            request.setAttribute ( "quoteTypes", QuotationDAO.getQuoteTypeLabelValueList( null ) );
            request.setAttribute ( "applications", QuotationDAO.getQuoteApplicationLabelValueList( null ) );
            request.setAttribute ( "screenDisplayFiledsList", QuotationDAO.getScreenDisplayFiledsList( null ) );

            request.setAttribute ( "QuotationForm", subform );
            saveToken ( request );
            return ( mapping.findForward ( "Quotation" ) );
        } // End of loadShippingInstructions

        else if ( "loadEndCustomerDefualtShippingAddress".equals ( action ) )
        {
            ArrayList shippingList = new ArrayList();
            if ( ( subform.getShippingAddressId () == -1 ) && ( session.getAttribute ( "newShippingAddressId" ) != null ) )
            {
                subform.setShippingAddressId ( Integer.parseInt ( ( String ) session.getAttribute ( "newShippingAddressId" ) ) );
                Address address = CustomerDAO.getAddress( subform.getShippingAddressId() );
                loadShippingAddress( address, shippingList, quotationFromMap );
            }
            quotationFromMap.setShippingAddresses(shippingList);
            int shippingAddressId = 0 ;

            if ( subform.getIsEndCustomerShipAddress() == false )
            {
                NewSalesOrderDAO.loadCustomerAddressesOfType(shippingList, subform.getCustomerId(), "Shipping");
                shippingAddressId = SalesOrderDAO.getDefaultShippingAddressId( subform.getCustomerId () );
            }
            else
            {
                if ( subform.getOdmId() > 0 )
                {
                    NewSalesOrderDAO.loadCustomerAddressesOfType(shippingList, subform.getOdmId(), "Shipping");
                    shippingAddressId = SalesOrderDAO.getDefaultShippingAddressId( subform.getOdmId () );
                }
                else
                {
                    NewSalesOrderDAO.loadCustomerAddressesOfType(shippingList, subform.getCustomerId(), "Shipping");
                    shippingAddressId = SalesOrderDAO.getDefaultShippingAddressId( subform.getCustomerId () );
                }
            }
            String formContactPerson = null;
            if ( NewSalesOrderDAO.isAddressIdExists( shippingList, subform.getShippingAddressId() ) )
            {
                formContactPerson = subform.getShippingContactPerson();
                shippingAddressId = subform.getShippingAddressId();
            }
            else
            {
                subform.setShippingAddressId(shippingAddressId);
            }

            Address shipAddress = CustomerDAO.getAddress ( shippingAddressId );

            HashMap shippingAddressMap = new HashMap();
            if(shipAddress != null)
            {
                /*String shipAddressWithoutContactPerson = SalesOrderAction.getAddressAsString( shipAddress );
                if ( "".equals( shipAddressWithoutContactPerson.trim() ) )
                {
                    int defaultShippingAddressId = SalesOrderDAO.getDefaultShippingAddressId( subform.getCustomerId () );
                    if ( defaultShippingAddressId != shippingAddressId )
                    {
                        shippingAddressId = defaultShippingAddressId;
                        subform.setShippingAddressId( shippingAddressId);
                        shipAddress = CustomerDAO.getAddress ( shippingAddressId );
                        subform.setShippingContactPerson( shipAddress.getContactPerson() );
                        formContactPerson = null;
                    }
                }*/
                if ( formContactPerson != null )
                {
                    subform.setShippingContactPerson( formContactPerson );
                }
                else
                {
                    subform.setShippingContactPerson( shipAddress.getContactPerson() );
                }
                ContactPerson shippingContactPerson = CustomerDAO.getAddressContactPersonDetails (shippingAddressId , subform.getShippingContactPerson());
                if( shippingContactPerson != null )
                {
                    shipAddress.setContactPerson(shippingContactPerson.getContactPerson());
                    shipAddress.setPhoneNumber(shippingContactPerson.getPhoneNumber());
                    shipAddress.setCellNumber(shippingContactPerson.getCellNumber());
                    shipAddress.setFaxNumber(shippingContactPerson.getFaxNumber());
                    shipAddress.setEmail(shippingContactPerson.getEmail());
                }
                else
                {
                    shipAddress.setContactPerson( "" );
                    shipAddress.setPhoneNumber("");
                    shipAddress.setCellNumber("");
                    shipAddress.setFaxNumber("");
                    shipAddress.setEmail("");
                }
                subform.setShippingAddress ( shipAddress.getAddressAsString ( ));
                shippingAddressMap = CustomerDAO.getAddressDetailsMap ( shippingAddressId, shipAddress);
                shippingAddressMap.put("",shipAddress.getAddressWithoutContactPersonAsString());
            }
            quotationFromMap.setShippingAddressMap( shippingAddressMap );
            quotationFromMap.setContactPersonsForShippingAddress( CustomerDAO.getAllContactPersonsForAddress(  shippingAddressId ) );
            ArrayList lineItems = null;

            lineItems = loadLineItems ( request, subform.getCustomerId (), errors, info );
            request.setAttribute ( "lineItems", lineItems );

            request.setAttribute ( "lineItemsWithSubtotals", SalesOrderAction.loadLineItemsWithSubTotals ( lineItems ) );
            session.setAttribute ( "maxLineItemNo", request.getAttribute ( "maxLineItemNo" ) );

            subform.setSalesTaxAsDouble(Double.parseDouble((String)request.getAttribute("salesTax")) );
            subform.setSubTotalAsDouble(Double.parseDouble((String)request.getAttribute("subTotal")) );
            subform.setOrderTotalAsDouble(subform.getSubTotalAsDouble()+subform.getFrieghtAsDouble()+subform.getMiscAsDouble()+subform.getSalesTaxAsDouble());
            session.setAttribute ( "allShippingTerms", session.getAttribute ( "allShippingTerms" ) );
            if ( subform.getOdmId() > 0 )
            {
                quotationFromMap.setSalesPersons( NewCustomerDAO.loadAllSalesPersonsforCustomerAndOdm( subform.getCustomerId(), subform.getOdmId() ) ) ;
            }
            else
            {
                quotationFromMap.setSalesPersons( CustomerDAO.loadAllSalesPersonsforCustomer( subform.getCustomerId ())) ;
            }
            quotationFromMap.setMfgPartsWithCustomerPartNos( NewSalesOrderDAO.loadManfacturingPartNumbersWithCustomerPartNumbers ( subform.getCustomerId (), subform.getOdmId(), isSample ) );
            quotationFromMap.setShippingMethods( CustomerDAO.loadShippingMethodsForCustomerAndOdm( subform.getCustomerId(), subform.getOdmId() ));
            request.setAttribute ( "Account", subform.getShippingAccount () );
            request.setAttribute ( "Method", subform.getShippingAccount () );
            request.setAttribute ( "quoteTypes", QuotationDAO.getQuoteTypeLabelValueList( null ) );
            request.setAttribute ( "applications", QuotationDAO.getQuoteApplicationLabelValueList( null ) );
            request.setAttribute ( "screenDisplayFiledsList", QuotationDAO.getScreenDisplayFiledsList( null ) );

            request.setAttribute ( "QuotationForm", subform );
            saveToken ( request );
            return ( mapping.findForward ( "Quotation" ) );
        } // End of loadEndCustomerDefualtShippingAddress
        else if ( "loadFreightInstructions".equals ( action ) )
        {
            if ( ( subform.getShippingAddressId () == -1 ) && ( session.getAttribute ( "newShippingAddressId" ) != null ) )
            {
                subform.setShippingAddressId ( Integer.parseInt ( ( String ) session.getAttribute ( "newShippingAddressId" ) ) );
                Address address = CustomerDAO.getAddress( subform.getShippingAddressId() );
                loadShippingAddress( address, quotationFromMap.getShippingAddresses(), quotationFromMap );
            }
            ArrayList lineItems = null;
            String freightInstructions = null;
            try
            {
                freightInstructions = NewSalesOrderDAO.loadSelectedFreightInstructions( null, subform.getFreightInstructionsId() );
            }
            catch ( ScalarException e )
            {
                System.out.println ( "Exception while loading selected freight instructions" );
                e.printStackTrace();
            }
            subform.setFreightInstructions ( freightInstructions );

            lineItems = loadLineItems ( request, subform.getCustomerId (), errors, info );
            request.setAttribute ( "lineItems", lineItems );

            request.setAttribute ( "lineItemsWithSubtotals", SalesOrderAction.loadLineItemsWithSubTotals ( lineItems ) );
            session.setAttribute ( "maxLineItemNo", request.getAttribute ( "maxLineItemNo" ) );

            subform.setSalesTaxAsDouble(Double.parseDouble((String)request.getAttribute("salesTax")) );
            subform.setSubTotalAsDouble(Double.parseDouble((String)request.getAttribute("subTotal")) );
            subform.setOrderTotalAsDouble(subform.getSubTotalAsDouble()+subform.getFrieghtAsDouble()+subform.getMiscAsDouble()+subform.getSalesTaxAsDouble());
            session.setAttribute ( "allShippingTerms", session.getAttribute ( "allShippingTerms" ) );
            if ( subform.getOdmId() > 0 )
            {
                quotationFromMap.setSalesPersons( NewCustomerDAO.loadAllSalesPersonsforCustomerAndOdm( subform.getCustomerId(), subform.getOdmId() ) ) ;
            }
            else
            {
                quotationFromMap.setSalesPersons( CustomerDAO.loadAllSalesPersonsforCustomer( subform.getCustomerId ())) ;
            }
            quotationFromMap.setShippingMethods( CustomerDAO.loadShippingMethodsForCustomerAndOdm( subform.getCustomerId(), subform.getOdmId() ));
            request.setAttribute ( "Account", subform.getShippingAccount () );
            request.setAttribute ( "Method", subform.getShippingAccount () );
            request.setAttribute ( "quoteTypes", QuotationDAO.getQuoteTypeLabelValueList( null ) );
            request.setAttribute ( "applications", QuotationDAO.getQuoteApplicationLabelValueList( null ) );
            request.setAttribute ( "screenDisplayFiledsList", QuotationDAO.getScreenDisplayFiledsList( null ) );

            request.setAttribute ( "QuotationForm", subform );
            saveToken ( request );
            return ( mapping.findForward ( "Quotation" ) );
        } // End of loadFreightInstructions
        else if ( "splitLineItems".equalsIgnoreCase( action ) )
        {
            if ( ( subform.getShippingAddressId () == -1 ) && ( session.getAttribute ( "newShippingAddressId" ) != null ) )
            {
                subform.setShippingAddressId ( Integer.parseInt ( ( String ) session.getAttribute ( "newShippingAddressId" ) ) );
                Address address = CustomerDAO.getAddress( subform.getShippingAddressId() );
                loadShippingAddress( address, quotationFromMap.getShippingAddresses(), quotationFromMap );
            }
            //---
            session.setAttribute ( "QuotationForm", subform );
            if ( subform.getOdmId() > 0 )
            {
                quotationFromMap.setSalesPersons( NewCustomerDAO.loadAllSalesPersonsforCustomerAndOdm( subform.getCustomerId(), subform.getOdmId() ) ) ;
            }
            else
            {
                quotationFromMap.setSalesPersons( CustomerDAO.loadAllSalesPersonsforCustomer( subform.getCustomerId ())) ;
            }
            ArrayList lineItems = loadLineItems ( request, subform.getCustomerId (), errors, info );
            session.setAttribute ( "lineItems", lineItems );
            session.setAttribute ( "lineItemsWithSubtotals", SalesOrderAction.loadLineItemsWithSubTotals ( lineItems ) );
            session.setAttribute ( "maxLineItemNo", request.getAttribute ( "maxLineItemNo" ) );
            request.setAttribute ( "Account", subform.getShippingAccount () );
            subform.setTotalQty ( Integer.parseInt ( ( String ) request.getAttribute ( "totalQty" ) ) );
            subform.setSalesTaxAsDouble(Double.parseDouble((String)request.getAttribute("salesTax")) );
            subform.setSubTotalAsDouble(Double.parseDouble((String)request.getAttribute("subTotal")) );
            subform.setOrderTotalAsDouble(subform.getSubTotalAsDouble()+subform.getFrieghtAsDouble()+subform.getMiscAsDouble()+subform.getSalesTaxAsDouble());

            session.setAttribute ( "totalQty", request.getAttribute ( "totalQty" ) );
            session.setAttribute ( "salesTax", request.getAttribute ( "salesTax" ) );
            session.setAttribute ( "subTotal", request.getAttribute ( "subTotal" ) );
            session.setAttribute ( "orderTotal", Double.toString ( subform.getSalesTaxAsDouble () + subform.getFrieghtAsDouble () + subform.getMiscAsDouble () + subform.getSubTotalAsDouble () ) );
            session.setAttribute ( "tempShippingMethod", subform.getShippingMethod () );
            subform.setOdm( CustomerDAO.getCustomerName(null,subform.getOdmId()) );
            subform.setEndCustomer( CustomerDAO.getCustomerName( null, subform.getEndCustomerId() ) );

            errors = ( HashMap ) request.getAttribute ( "Errors" );
            if ( !errors.isEmpty () )
            {
                request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
                return ( mapping.findForward ( "Quotation" ) );
            }

            info = ( HashMap ) request.getAttribute ( "Info" );
            if ( !info.isEmpty () )
            {
                request.setAttribute ( SessionKeys.INFO_MESSAGES, info );
            }
            saveToken ( request );
            return new ActionForward("/splitQuotation.do?quotationId="+subform.getQuotationId()+"&customerId="+subform.getCustomerId(), false);
        }



        else if ( "quotationWithQuestionnaire".equalsIgnoreCase( action ) )
        {
            if (subform.getQuotationId() > 0 )
            {
                String quotationId = request.getParameter ( "quotationId" );
                QuotationQuestionnairy quotationQuestionnairy = subform.getQuotationQuestionnairy();
                Quotation quotation = QuotationDAO.loadQuotation ( quotationId, false );
                if ( quotationMap == null )
                {
                        quotationMap = new HashMap();
                }
                quotationMap.put ( quotation.getCustomerId()+"", quotation );
                session.setAttribute ( "quotationMap", quotationMap ) ;
                session.setAttribute ( "oldQuotation", quotation );
                Address addressFromQuotation = quotation.getBillingAddressAsObject();
                Quotation quotationCustomerDetails = QuotationDAO.getQuotationCustomerDetails ( Integer.toString ( quotation.getCustomerId () ) );
                quotation.setShippingMethods ( quotationCustomerDetails.getShippingMethods () );
                ArrayList shippingAddressesList = new ArrayList();
                if ( quotation.getIsEndCustomerShipAddress  () )
                {
                    if ( quotation.getOdmId() > 0 )
                    {
                        NewSalesOrderDAO.loadCustomerAddressesOfType(shippingAddressesList, quotation.getOdmId(), "Shipping");
                    }
                    else
                    {
                        NewSalesOrderDAO.loadCustomerAddressesOfType(shippingAddressesList, quotation.getCustomerId(), "Shipping");
                    }
                }
                else
                {
                    NewSalesOrderDAO.loadCustomerAddressesOfType(shippingAddressesList, quotation.getCustomerId(), "Shipping");
                }
                quotation.setShippingAddresses ( shippingAddressesList );
                //System.out.println("quotation = " + quotation.getQuotationId());
                //Copying the properties into the form object
                PropertyUtils.copyProperties ( subform, quotation );
                subform.setQuotationQuestionnairy(quotationQuestionnairy);
                subform.setRevision( QuotationDAO.loadRevisionNumber( null, quotation.getQuotationId() ) + 1 );

                Address customerAddressObject = null;
                customerAddressObject = QuotationDAO.getQuotationAddress ( null,quotation.getQuotationId() );
                if( customerAddressObject == null )
                {
                    customerAddressObject = CustomerDAO.getAddress ( quotationCustomerDetails.getBillingAddressId () );
                }
                ContactPerson customerContactPerson = new ContactPerson();
                customerContactPerson.setContactPerson(subform.getCustomerName());
                customerContactPerson.setPhoneNumber(customerAddressObject.getPhoneNumber());
                customerContactPerson.setCellNumber("");
                customerContactPerson.setFaxNumber(customerAddressObject.getFaxNumber());
                customerContactPerson.setEmail(customerAddressObject.getEmail());
                HashMap customerContactPersonMap = CustomerDAO.getContactPersonDetailsMap ( quotation.getCustomerId());
                customerContactPersonMap.put(subform.getCustomerName(),customerContactPerson);
                quotation.setCustomerContactPersonMap( customerContactPersonMap );

                HashMap billingAddressMap = CustomerDAO.getAddressDetailsMap ( quotation.getBillingAddressId() , quotation.getBillingAddressAsObject ());
                addressFromQuotation.setContactPerson("");
                addressFromQuotation.setPhoneNumber(customerAddressObject.getPhoneNumber());
                addressFromQuotation.setCellNumber("");
                addressFromQuotation.setFaxNumber(customerAddressObject.getFaxNumber());
                addressFromQuotation.setEmail(customerAddressObject.getEmail());
                billingAddressMap.put("",addressFromQuotation.getAddressAsString());
                quotation.setBillingAddressMap( billingAddressMap );

                HashMap shippingAddressMap = new HashMap();
                int shippingAddressId = quotation.getShippingAddressId ();
                Address shipAddress = CustomerDAO.getAddress ( shippingAddressId );
                shippingAddressMap = CustomerDAO.getAddressDetailsMap ( shippingAddressId , shipAddress );
                shippingAddressMap.put("",shipAddress.getAddressWithoutContactPersonAsString());
                quotation.setShippingAddressMap( shippingAddressMap );

                request.setAttribute ( "shippingAddresses", quotationCustomerDetails.getShippingAddresses () );
                quotation.setShippingAddresses( quotationCustomerDetails.getShippingAddresses () );
                if ( subform.getOdmId() > 0 )
                {
                    quotation.setMfgPartsWithCustomerPartNos( NewSalesOrderDAO.loadManfacturingPartNumbersWithCustomerPartNumbers ( subform.getCustomerId (), subform.getOdmId(), isSample ) );
                    quotation.setMfgPartsWithLabelNumbers( NewSalesOrderDAO.loadManfacturingPartNumbersWithOdmLabelNumbers ( subform.getCustomerId (), subform.getOdmId(), isSample ) );
                    quotation.setSalesPersons( NewCustomerDAO.loadAllSalesPersonsforCustomerAndOdm( subform.getCustomerId(), subform.getOdmId() ) );
                    quotation.setShippingMethods( CustomerDAO.loadShippingMethodsForCustomerAndOdm ( subform.getCustomerId(), subform.getOdmId() ) );
                    subform.setShippingMethods( CustomerDAO.loadShippingMethodsForCustomerAndOdm ( subform.getCustomerId(), subform.getOdmId()  ) );
                    quotation.setOdmList( CustomerRelationshipDAO.getOdmsForChangeSalesOrder( quotation.getCustomerId()+"" , quotation.getOdmId()+"" ) );
                }
                else if ( ! subform.getEndUser ().trim ().equals ( "" ) )
                {
                    EndUser endUser = EndUserDAO.getEndUserDetails ( Integer.parseInt ( subform.getEndUserId () ) );
                    request.setAttribute ( "mfgPartsWithCustomerPartNos", EndUserDAO.loadManfacturingPartNumbersWithEndUserPartNos ( Integer.parseInt ( subform.getEndUserId () ) ) );
                    request.setAttribute ( "mfgPartsWithLabelNumbers", EndUserDAO.loadManfacturingPartNumbersWithEndUserLabelNumbers ( Integer.parseInt ( subform.getEndUserId () ) ) );
                    ArrayList shippingAddresses = endUser.getShippingLocations ();
                    shippingAddresses.add ( 0, endUser.getShippingAddress () );
                    shippingAddresses.add ( CustomerDAO.getAddress ( SalesOrderDAO.getDefaultShippingAddressId( subform.getCustomerId() ) ) );
                    subform.setShippingAddresses ( loadShippingAddresses ( shippingAddresses ) );
                    subform.setShippingMethods( CustomerDAO.loadSalesOrderShippingMethods ( subform.getCustomerId() ) );
                }
                else
                {
                    subform.setShippingMethods( CustomerDAO.loadSalesOrderShippingMethods ( subform.getCustomerId() ) );
                    quotation.setShippingMethods( subform.getShippingMethods () );
                    quotation.setMfgPartsWithCustomerPartNos(  NewSalesOrderDAO.loadManfacturingPartNumbersWithCustomerPartNumbers ( quotation.getCustomerId (), subform.getOdmId(), isSample ) );
                    quotation.setMfgPartsWithLabelNumbers( NewSalesOrderDAO.loadManfacturingPartNumbersWithOdmLabelNumbers ( quotation.getCustomerId (), subform.getOdmId(), isSample ) );
                    quotation.setSalesPersons( CustomerDAO.loadAllSalesPersonsforCustomer(subform.getCustomerId ()) );
                }
                if ( subform.getShippingMethodName () != null )
                {
                    subform.setShippingMethodName ( subform.getShippingMethodName ().trim () );
                }
                if ( subform.getShippingAccount () != null )
                {
                    subform.setShippingAccount ( subform.getShippingAccount ().trim () );
                }
                ArrayList lineItems = QuotationDAO.loadOrderLineItemsWithCustomerPartNo ( Integer.parseInt ( quotationId ), quotation.getCustomerId () );
                quotation.setMfgParts( SalesOrderDAO.loadAllManfacturingPartNosOfCustomer ( quotation.getCustomerId (), isSample ) );
                quotation.setMfgPartsWithComments( SalesOrderDAO.loadAllManfacturingPartNumbersWithComments ( quotation.getCustomerId (), isSample ) );
                quotation.setSalesReps( CustomerDAO.loadAllSalesRepsforCustomer(quotation.getCustomerId () ) );
                quotation.setOdmList( CustomerRelationshipDAO.getAllOdmsForCustomer( quotation.getCustomerId ()+"") );
                if ( quotation.getEndCustomerId() > 0 )
                {
                    quotation.setEndCustomerList( CustomerRelationshipDAO.getEndCustomersForChangeSalesOrder( quotation.getCustomerId()+"" , quotation.getEndCustomerId()+"" ) );
                }
                else
                {
                    quotation.setEndCustomerList( CustomerRelationshipDAO.getAllEndCustomersForCustomer( quotation.getCustomerId()+"" ) );
                }


                quotation.setChangedLineItems( QuotationDAO.loadChangedLineItems( null, quotation.getQuotationId(), 1 )  );
                session.setAttribute("questionnairefile"+ subform.getQuotationNo(), subform.getContractPriceFilePath());
                request.setAttribute ( "quoteTypes", QuotationDAO.getQuoteTypeLabelValueList( null ) );
                request.setAttribute ( "applications", QuotationDAO.getQuoteApplicationLabelValueList( null ) );
                request.setAttribute ( "screenDisplayFiledsList", QuotationDAO.getScreenDisplayFiledsList( null ) );
                request.setAttribute ( "Account", subform.getShippingMethodName () + "   #   " + subform.getShippingAccount () );
                request.setAttribute ( "Method", subform.getShippingMethodName () + "   #   " + subform.getShippingAccount () );
                session.setAttribute ( "QuotationForm", subform );
                session.setAttribute ( "allShippingTerms", quotation.getAllShippingTerms () );
                session.setAttribute ( "allDeliveryTerms", quotation.getAllDeliveryTerms() );
                session.setAttribute ( "shippingInstructions", NewSalesOrderDAO.loadShippingInstructions( null ) );

                request.setAttribute ( "lineItems", lineItems );
                session.setAttribute ( "lineItemsWithSubtotals", SalesOrderAction.loadLineItemsWithSubTotals ( lineItems ) );
                session.setAttribute ( "maxLineItemNo", Integer.toString ( QuotationDAO.loadMaxLineItemNo ( quotation.getQuotationId () ) ) );
                session.setAttribute ( "salesTaxes", SalesDAO.loadSalesTaxList () );
                session.setAttribute ( "freightInstructions", NewSalesOrderDAO.loadFreightInstructions( null ) );
                session.setAttribute ( "odmList", quotation.getOdmList());
                session.setAttribute ( "endCustomerList", quotation.getEndCustomerList());
                session.setAttribute ( "quotationTypesList", QuotationAction.getQuotationTypesList() );
                session.setAttribute ( "qtyPerOptionsList", QuotationAction.getQtyPerOptionsList());
                session.setAttribute ( "commitmentsList", QuotationAction.getCommitmentsList());
                session.setAttribute ( "quantityiesList", QuotationAction.getQuantityiesList());
                session.setAttribute ( "priceDiscountOptionsList", QuotationAction.getPriceDiscountOptionsList());
                session.setAttribute ( "rebateGoalsList", QuotationAction.getRebateGoalsList());
                session.setAttribute ( "competitorsList", QuotationDAO.getCompetitors(null));
                request.setAttribute ( "QuotationForm", subform );
                saveToken ( request );
                PropertyUtils.copyProperties ( quotation, subform );

            return ( mapping.findForward ( "Quotation" ) );
            }
        }
        else if ( request.getParameter ( "quotationId" ) != null )
        {
            String quotationId = request.getParameter ( "quotationId" );
            ArrayList prevQuotationList;
            ArrayList prevQuotationPriceList;
            Quotation quotation = QuotationDAO.loadQuotation ( quotationId, false );
            if ( quotationMap == null ) {
                    quotationMap = new HashMap();
            }
            if( ConfigManager.isLoadQuotationQuestionnaire() )
            {
                QuotationDAO.loadQuotationQuestionnaireDetails( null, quotation );

                QuotationDAO.retrieveAttachedFileInformation( quotation );                
            }
            quotationMap.put( quotation.getCustomerId()+"", quotation );
            session.setAttribute( "quotationMap", quotationMap ) ;
            session.setAttribute ( "oldQuotation", quotation );

            Address addressFromQuotation = quotation.getBillingAddressAsObject();
            Quotation quotationCustomerDetails = QuotationDAO.getQuotationCustomerDetails ( Integer.toString ( quotation.getCustomerId () ) );

            quotation.setShippingMethods ( quotationCustomerDetails.getShippingMethods () );
            ArrayList shippingAddressesList = new ArrayList();
            if ( quotation.getIsEndCustomerShipAddress() )
            {
                if ( quotation.getOdmId() > 0 )
                {
                    NewSalesOrderDAO.loadCustomerAddressesOfType(shippingAddressesList, quotation.getOdmId(), "Shipping");
                }
                else
                {
                    NewSalesOrderDAO.loadCustomerAddressesOfType(shippingAddressesList, quotation.getCustomerId(), "Shipping");
                }
            }
            else
            {
                NewSalesOrderDAO.loadCustomerAddressesOfType(shippingAddressesList, quotation.getCustomerId(), "Shipping");
            }
            quotation.setShippingAddresses ( shippingAddressesList );
            //Copying the properties into the form object
            PropertyUtils.copyProperties ( subform, quotation );

            subform.setRevision( QuotationDAO.loadRevisionNumber( null, quotation.getQuotationId() ) + 1 );

            Address customerAddressObject = null;
            customerAddressObject = QuotationDAO.getQuotationAddress ( null,quotation.getQuotationId() );
            if( customerAddressObject == null )
            {
                customerAddressObject = CustomerDAO.getAddress ( quotationCustomerDetails.getBillingAddressId () );
            }

            ContactPerson customerContactPerson = new ContactPerson();
            customerContactPerson.setContactPerson(subform.getCustomerName());
            customerContactPerson.setPhoneNumber(customerAddressObject.getPhoneNumber());
            customerContactPerson.setCellNumber("");
            customerContactPerson.setFaxNumber(customerAddressObject.getFaxNumber());
            customerContactPerson.setEmail(customerAddressObject.getEmail());
            HashMap customerContactPersonMap = CustomerDAO.getContactPersonDetailsMap ( quotation.getCustomerId());
            customerContactPersonMap.put(subform.getCustomerName(),customerContactPerson);
            quotation.setCustomerContactPersonMap( customerContactPersonMap );

            HashMap billingAddressMap = CustomerDAO.getAddressDetailsMap ( quotation.getBillingAddressId() , quotation.getBillingAddressAsObject ());
            addressFromQuotation.setContactPerson("");
            addressFromQuotation.setPhoneNumber(customerAddressObject.getPhoneNumber());
            addressFromQuotation.setCellNumber("");
            addressFromQuotation.setFaxNumber(customerAddressObject.getFaxNumber());
            addressFromQuotation.setEmail(customerAddressObject.getEmail());
            billingAddressMap.put("",addressFromQuotation.getAddressAsString());
            quotation.setBillingAddressMap( billingAddressMap );

            HashMap shippingAddressMap = new HashMap();
            int shippingAddressId = quotation.getShippingAddressId ();
            Address shipAddress = CustomerDAO.getAddress ( shippingAddressId );
            shippingAddressMap = CustomerDAO.getAddressDetailsMap ( shippingAddressId , shipAddress );
            shippingAddressMap.put("",shipAddress.getAddressWithoutContactPersonAsString());
            quotation.setShippingAddressMap( shippingAddressMap );
            request.setAttribute ( "shippingAddresses", quotationCustomerDetails.getShippingAddresses () );
            quotation.setShippingAddresses( quotationCustomerDetails.getShippingAddresses () );
            if ( subform.getOdmId() > 0 )
            {
                quotation.setMfgPartsWithCustomerPartNos( NewSalesOrderDAO.loadManfacturingPartNumbersWithCustomerPartNumbers ( subform.getCustomerId (), subform.getOdmId(), isSample ) );
                quotation.setMfgPartsWithLabelNumbers( NewSalesOrderDAO.loadManfacturingPartNumbersWithOdmLabelNumbers ( subform.getCustomerId (), subform.getOdmId(), isSample ) );
                quotation.setSalesPersons( NewCustomerDAO.loadAllSalesPersonsforCustomerAndOdm( subform.getCustomerId(), subform.getOdmId() ) );
                quotation.setShippingMethods( CustomerDAO.loadShippingMethodsForCustomerAndOdm ( subform.getCustomerId(), subform.getOdmId() ) );
                subform.setShippingMethods( CustomerDAO.loadShippingMethodsForCustomerAndOdm ( subform.getCustomerId(), subform.getOdmId()  ) );
                quotation.setOdmList( CustomerRelationshipDAO.getOdmsForChangeSalesOrder( quotation.getCustomerId()+"" , quotation.getOdmId()+"" ) );
            }
            else if ( ! subform.getEndUser ().trim ().equals ( "" ) )
            {
                EndUser endUser = EndUserDAO.getEndUserDetails ( Integer.parseInt ( subform.getEndUserId () ) );
                request.setAttribute ( "mfgPartsWithCustomerPartNos", EndUserDAO.loadManfacturingPartNumbersWithEndUserPartNos ( Integer.parseInt ( subform.getEndUserId () ) ) );
                request.setAttribute ( "mfgPartsWithLabelNumbers", EndUserDAO.loadManfacturingPartNumbersWithEndUserLabelNumbers ( Integer.parseInt ( subform.getEndUserId () ) ) );
                ArrayList shippingAddresses = endUser.getShippingLocations ();
                shippingAddresses.add ( 0, endUser.getShippingAddress () );
                shippingAddresses.add ( CustomerDAO.getAddress ( SalesOrderDAO.getDefaultShippingAddressId( subform.getCustomerId() ) ) );
                subform.setShippingAddresses ( loadShippingAddresses ( shippingAddresses ) );
                subform.setShippingMethods( CustomerDAO.loadSalesOrderShippingMethods ( subform.getCustomerId() ) );
            }
            else
            {
                subform.setShippingMethods( CustomerDAO.loadSalesOrderShippingMethods ( subform.getCustomerId() ) );
                quotation.setShippingMethods( subform.getShippingMethods () );
                quotation.setMfgPartsWithCustomerPartNos(  NewSalesOrderDAO.loadManfacturingPartNumbersWithCustomerPartNumbers ( quotation.getCustomerId (), subform.getOdmId(), isSample ) );
                quotation.setMfgPartsWithLabelNumbers( NewSalesOrderDAO.loadManfacturingPartNumbersWithOdmLabelNumbers ( quotation.getCustomerId (), subform.getOdmId(), isSample ) );
                quotation.setSalesPersons( CustomerDAO.loadAllSalesPersonsforCustomer(subform.getCustomerId ()) );
            }
            if ( subform.getShippingMethodName () != null )
            {
                subform.setShippingMethodName ( subform.getShippingMethodName ().trim () );
            }
            if ( subform.getShippingAccount () != null )
            {
                subform.setShippingAccount ( subform.getShippingAccount ().trim () );
            }

            ArrayList lineItems = QuotationDAO.loadOrderLineItemsWithCustomerPartNo ( Integer.parseInt ( quotationId ), quotation.getCustomerId () );

            quotation.setMfgParts( SalesOrderDAO.loadAllManfacturingPartNosOfCustomer ( quotation.getCustomerId (), isSample ) );
            quotation.setMfgPartsWithComments( SalesOrderDAO.loadAllManfacturingPartNumbersWithComments ( quotation.getCustomerId (), isSample ) );
            quotation.setSalesReps( CustomerDAO.loadAllSalesRepsforCustomer(quotation.getCustomerId () ) );
            quotation.setOdmList( CustomerRelationshipDAO.getAllOdmsForCustomer( quotation.getCustomerId ()+"") );
            if ( quotation.getEndCustomerId() > 0 )
            {
                quotation.setEndCustomerList( CustomerRelationshipDAO.getEndCustomersForChangeSalesOrder( quotation.getCustomerId()+"" , quotation.getEndCustomerId()+"" ) );
            }
            else
            {
                quotation.setEndCustomerList( CustomerRelationshipDAO.getAllEndCustomersForCustomer( quotation.getCustomerId()+"" ) );
            }


            quotation.setChangedLineItems( QuotationDAO.loadChangedLineItems( null, quotation.getQuotationId(), 1 )  );
            
            request.setAttribute ( "quoteTypes", QuotationDAO.getQuoteTypeLabelValueList( null ) );
            request.setAttribute ( "applications", QuotationDAO.getQuoteApplicationLabelValueList( null ) );
            request.setAttribute ( "screenDisplayFiledsList", QuotationDAO.getScreenDisplayFiledsList( null ) );
            request.setAttribute ( "Account", subform.getShippingMethodName () + "   #   " + subform.getShippingAccount () );
            request.setAttribute ( "Method", subform.getShippingMethodName () + "   #   " + subform.getShippingAccount () );
            session.setAttribute ( "QuotationForm", subform );
            session.setAttribute ( "allShippingTerms", quotation.getAllShippingTerms () );
            session.setAttribute ( "allDeliveryTerms", quotation.getAllDeliveryTerms() );
            session.setAttribute ( "shippingInstructions", NewSalesOrderDAO.loadShippingInstructions( null ) );

            request.setAttribute ( "lineItems", lineItems );
            session.setAttribute ( "lineItemsWithSubtotals", SalesOrderAction.loadLineItemsWithSubTotals ( lineItems ) );
            session.setAttribute ( "maxLineItemNo", Integer.toString ( QuotationDAO.loadMaxLineItemNo ( quotation.getQuotationId () ) ) );
            session.setAttribute ( "salesTaxes", SalesDAO.loadSalesTaxList () );
            session.setAttribute ( "freightInstructions", NewSalesOrderDAO.loadFreightInstructions( null ) );
            session.setAttribute ( "odmList", quotation.getOdmList());
            session.setAttribute ( "endCustomerList", quotation.getEndCustomerList());
            session.setAttribute ( "quotationTypesList", QuotationAction.getQuotationTypesList() );
            session.setAttribute ( "qtyPerOptionsList", QuotationAction.getQtyPerOptionsList());
            session.setAttribute ( "commitmentsList", QuotationAction.getCommitmentsList());
            session.setAttribute ( "quantityiesList", QuotationAction.getQuantityiesList());
            session.setAttribute ( "priceDiscountOptionsList", QuotationAction.getPriceDiscountOptionsList());
            session.setAttribute ( "rebateGoalsList", QuotationAction.getRebateGoalsList());
            session.setAttribute ( "competitorsList", QuotationDAO.getCompetitors(null));
            prevQuotationList = QuotationDAO.getPrevQuotationDetails(null, subform.getCustomerId());
            prevQuotationPriceList = QuotationDAO.getPrevQuotationPriceDetails(null, subform.getCustomerId());
            request.setAttribute("prevQuotationsList", prevQuotationList);
            request.setAttribute("prevQuotationPriceList", prevQuotationPriceList);
            request.setAttribute( "prevCommitmentsDetails", QuotationDAO.getPrevCommitmentsDetails(null, subform.getCustomerId()));
            request.setAttribute ( "QuotationForm", subform );
            saveToken ( request );
            PropertyUtils.copyProperties ( quotation, subform );
            if (ConfigManager.isLoadQuotationQuestionnaire())
            {
                return ( mapping.findForward ( "QuotationQuestionnaire" ) );
            }
            else
            {
                return ( mapping.findForward ( "Quotation" ) );
            }
        }

        return ( mapping.findForward ( "successQuotation" ) );
    }


    public static ArrayList loadLineItems ( HttpServletRequest request, int customerId,
                                            HashMap errors, HashMap info ) throws ScalarException
    {
        HttpSession session = request.getSession ();

        String[] lineItem = request.getParameterValues ( "lineItemNo" );
        String[] lineItemId = request.getParameterValues ( "lineItemId" );
        String[] poLineItem = request.getParameterValues ( "poLineItemNo" );
        String[] manufacturingPartNo = request.getParameterValues ( "manufacturingPartNo" );
        String[] quantity = request.getParameterValues ( "quantity" );
        String[] minOrderQty = request.getParameterValues ( "minOrderQty" );
        String[] leadTime = request.getParameterValues ( "leadTime" );
        String[] oldQty = request.getParameterValues ( "oldQty" );
        String[] requestDate = request.getParameterValues ( "requestDate" );
        String[] unitPrice = request.getParameterValues ( "unitPriceAsDouble" );
        String[] reqPrice = request.getParameterValues ( "reqPrice" );
        String[] custResalePrice = request.getParameterValues ( "custResalePrice" );
        String[] suggResalePrice = request.getParameterValues ( "suggResalePrice" );
        String[] lineItemDescription = request.getParameterValues ( "lineItemDescription" );
        String[] customerPartNo = request.getParameterValues ( "customerPartNo" );
        String[] labelNumber = null;
        String[] isNewLineItem = request.getParameterValues ( "isNewLineItem" );
        String[] quotedDate = null;
        String[] quotedQty = null;
        String[] isApproved = request.getParameterValues ( "isApproved" );
        String[] newCustPartNo = request.getParameterValues ( "newCustomerPartNo" );
        ArrayList lineItemsList = new ArrayList ();
        LineItem lineItemInfo = null;

        String zeroPrice = "";
        String productPrice = "";

        int totalQty = 0;
        float subTotal = 0.0f;
        double salesTax = 0.0;
        double finalSubTotal = 0.0;

        String salesTaxType = request.getParameter ( "salesTaxType" );
        if ( ( salesTaxType.equalsIgnoreCase ( "-1" ) ) == true )
        {
            salesTaxType = request.getParameter ( "newSalesState" );
        }
        float taxvalue = SalesOrderDAO.getTaxValue ( null, salesTaxType );
        Float ff = new Float ( taxvalue );
        double tax = ff.doubleValue ();

        int tempLineItemNo = 0;
        for ( int i = 0; i < manufacturingPartNo.length; i++ )
        {
            if ( lineItem[i].trim ().equals ( "" ) )
                lineItem[i] = "0";
            if ( ( manufacturingPartNo[i] != null ) && ( !manufacturingPartNo[i].equals ( "" ) ) )
            {
                if ( quantity[i].equals ( "" ) )
                    quantity[i] = "0";
                if ( unitPrice[i].equals ( "" ) )
                    unitPrice[i] = "0.0";

                lineItemInfo = SalesOrderDAO.loadLineItemInfo ( customerId, Integer.parseInt ( lineItem[i] ),
                                                                null,
                                                                manufacturingPartNo[i],
                                                                requestDate[i], leadTime[i],
                                                                Integer.parseInt ( quantity[i] ),
                                                                StringUtils.formatToFloat ( unitPrice[i] ), 1);

                //It is not a valid information entered, so add it errors
                if ( lineItemInfo == null )
                {
                    errors.put ( "LineItem No " + lineItem[i], "is not having valid partnumbers, please enter valid partnumbers" );
                }

                //If the lineItem is not valid then it is null, then put all the values entered back to object
                //and sbow the error message to correct it
                if ( lineItemInfo == null )
                {
                    lineItemInfo = new LineItem ();
                    lineItemInfo.setLineItemNo ( Integer.parseInt ( lineItem[i] ) );
                    lineItemInfo.setManufacturingPartNo ( manufacturingPartNo[i] );
                    lineItemInfo.setLineItemDescription ( lineItemDescription[i].trim () );
                    lineItemInfo.setLeadTime(leadTime[i]);
                    lineItemInfo.setCustomerPartNo ( customerPartNo[i] );
                    lineItemInfo.setNewCustomerPartNo ( newCustPartNo[i] );
                    if(Constants.CLIENT_NAME_AIRGO.equalsIgnoreCase(ConfigManager.getClientNameAirgo() ) )
                    {
                        lineItemInfo.setLabelNumber( labelNumber[i] );
                    }
                    lineItemInfo.setQuantity ( Integer.parseInt ( quantity[i] ) );
                    lineItemInfo.setUnitPrice ( StringUtils.formatToFloat ( unitPrice[i] ) );
                    lineItemInfo.setUnitPriceAsDouble ( Double.parseDouble ( unitPrice[i] ) );
                    lineItemInfo.setRequestDate ( requestDate[i] );
                }
                else
                {
                    //Need to have the request date before you submit...request date is required field
                    if ( requestDate[i].trim ().length () == 0 )
                        errors.put ( "LineItem No " + lineItem[i], "Please enter Request  Dock Date." );
                }
                lineItemInfo.setIsApproved( Boolean.valueOf(isApproved[i]).booleanValue() );
                if ( request.getParameter ( "cancel" + lineItemInfo.getLineItemNo() ) != null )
                {
                    lineItemInfo.setIsCancel ( true );
                }
                if (minOrderQty != null && minOrderQty.length > i) {
                    try {
                        lineItemInfo.setMinOrderQty(Integer.parseInt(minOrderQty[i]));
                    } catch (NumberFormatException ignore) {
                    }
                }
                //If it is not sample need to set unit price and amount
                lineItemInfo.setPoLineItemNo ( Integer.parseInt ( poLineItem[i] ) ); // Added by Basha (08/18/2003)
                zeroPrice = request.getParameter ( "zeroPrice" + lineItem[i] );
                lineItemInfo.setZeroPrice ( ( zeroPrice != null ) ? true : false ); // Modified by Basha (09/22/2003)
                if ( "0".equals ( oldQty[i] ) )
                {
                    oldQty[i] = quantity[i];
                }
                productPrice = SalesOrderDAO.loadOldQtyPrice ( null, lineItemInfo.getProductId (), customerId, Integer.parseInt ( oldQty[i] ),lineItemInfo.getRequestDate(), 1);

                if ( ( ( StringUtils.formatToFloat ( unitPrice[i] ) != 0.0 ) || zeroPrice != null ) )
                {
                    if ( ( quantity[i].equalsIgnoreCase ( oldQty[i] ) ) || ( !unitPrice[i].equalsIgnoreCase ( productPrice ) ) )
                    {
                        lineItemInfo.setUnitPrice ( StringUtils.formatToFloat ( Double.toString ( Double.parseDouble ( unitPrice[i] ) ) ) );
                        lineItemInfo.setUnitPriceAsDouble ( Double.parseDouble ( unitPrice[i] ) );
                        double unit_price = Double.parseDouble ( unitPrice[i] );
                        double amt = ( Integer.parseInt ( quantity[i] ) ) * unit_price;
                        lineItemInfo.setAmountAsDouble ( amt );
                    }
                }
                try
                {
                    if ( lineItemId[i].trim ().length () != 0 )
                        lineItemInfo.setLineItemId ( Integer.parseInt ( lineItemId[i] ) );
                }
                catch ( ArrayIndexOutOfBoundsException aex )
                {
                    lineItemInfo.setLineItemId ( -1 );
                }
                lineItemInfo.setIsExpedite ( ( request.getParameter ( "expediteFee" + lineItem[i] ) != null ) ? true : false );

                if ( request.getParameter ( "isTaxable" + lineItem[i] ) != null )
                {
                    lineItemInfo.setIsTaxable ( true );
                    salesTax = salesTax + ( lineItemInfo.getAmountAsDouble () * ( tax / 100 ) );
                }
                if ( ( lineItemDescription[i] == null ) || ( lineItemDescription[i].trim ().length () == 0 ) )
                {
                    String lItemDesc = null;
                    try
                    {
                        lItemDesc = SalesOrderDAO.getLineItemDescriptionByMfgPartNumber ( manufacturingPartNo[i] );
                        lItemDesc = ( ( lItemDesc == null ) ? "" : lItemDesc );
                        lineItemInfo.setLineItemDescription ( lItemDesc );
                    }
                    catch ( ScalarException se )
                    {
                        errors.put ( ERROR_INTERNAL_SERVER_ERROR, ERROR_IN_SAVING_SALES_ORDER_DETAILS );
                        errors.put ( ERROR_MESSAGE_CONTACT_SYS_ADMN, null );
                        session.setAttribute ( SessionKeys.ERROR_REPORT_SUBJECT, " Error: Could Not Save Sales Order Details " );
                        session.setAttribute ( SessionKeys.ERROR_REPORT, se );
                    }

                }
                else
                {
                    lineItemInfo.setLineItemDescription ( lineItemDescription[i] );
                }

                lineItemInfo.setCustomerPartNo ( customerPartNo[i] );
                lineItemInfo.setNewCustomerPartNo ( newCustPartNo[i] );
                if(Constants.CLIENT_NAME_AIRGO.equalsIgnoreCase(ConfigManager.getClientNameAirgo() ) )
                {
                    lineItemInfo.setLabelNumber( labelNumber[i] );
                }

                //calculate total quantity and amount and set it in request
                if(Constants.CLIENT_NAME_AIRGO.equalsIgnoreCase(ConfigManager.getClientNameAirgo() )){
                    if( quotedQty[i] != null && quotedQty[i].trim().length() > 0 ){
                        lineItemInfo.setQuotedQuantity( Integer.parseInt( quotedQty[i] ) );
                    }
                    lineItemInfo.setQuotedDate( quotedDate[i] );
                }
                if ( reqPrice != null && reqPrice.length > i )
                {
                    try
                    {
                        lineItemInfo.setReqPrice( Double.parseDouble ( reqPrice[i] ) );
                    }catch ( NumberFormatException ignore ) {}
                }
                if ( custResalePrice != null && custResalePrice.length > i )
                {
                    try
                    {
                        lineItemInfo.setCustResalePrice( Double.parseDouble ( custResalePrice[i] ) );
                    }catch ( NumberFormatException ignore ) {}
                }
                if ( suggResalePrice != null && suggResalePrice.length > i )
                {
                    try
                    {
                        lineItemInfo.setSuggResalePrice( Double.parseDouble ( suggResalePrice[i] ) );
                    }catch ( NumberFormatException ignore ) {}
                }
                totalQty = totalQty + lineItemInfo.getQuantity ();

                finalSubTotal = finalSubTotal + lineItemInfo.getAmountAsDouble ();

                try
                {
                    lineItemInfo.setAmount ( Float.parseFloat ( StringUtils.formatAmountWithNoOfDecimals ( lineItemInfo.getAmount ()) ) );
                }catch ( NumberFormatException ignore ) {}


                request.setAttribute ( "totalQty", Integer.toString ( totalQty ) );

                request.setAttribute ( "salesTax", Double.toString ( salesTax ) );
                request.setAttribute ( "subTotal", Double.toString ( finalSubTotal ) );
                if ( isNewLineItem[i].equalsIgnoreCase ( "true" ) )
                {
                    lineItemInfo.setIsNewLineItem ( true );
                }
                else
                {
                    lineItemInfo.setIsNewLineItem ( false );
                }
                if(Integer.parseInt(lineItem[i]) > tempLineItemNo )
                {
                    tempLineItemNo = Integer.parseInt(lineItem[i]);
                }
                lineItemsList.add ( lineItemInfo );
            }

        }
        request.setAttribute ( "maxLineItemNo", Integer.toString ( tempLineItemNo  + 1 ) );
        request.setAttribute ( "Errors", errors );
        request.setAttribute ( "Info", info );

        return lineItemsList;
    }

    private void addNewSalesTaxState ( HttpServletRequest request, HttpSession session, HashMap errors, QuotationForm subform )
    {
        String state1 = null;
        String value1 = null;
        String[] newSalesState = new String[1];
        String[] newSalesTax = new String[1];
        boolean checkstate = false;

        try
        {
            state1 = request.getParameter ( "newSalesState" );
            value1 = request.getParameter ( "newSalesTax" );
            String salesTaxType = request.getParameter ( "salesTaxType" );

            if ( "-1".equals ( salesTaxType ) )
            {
                checkstate = SalesOrderDAO.doesSalesTaxStateExist ( null, state1 );

                if ( checkstate == false )
                {
                    if ( ( state1 != null && state1.length () > 0 ) && ( value1 != null && value1.length () > 0 ) )
                    {
                        newSalesState[0] = state1;
                        newSalesTax[0] = value1;

                        SalesDAO.saveSalesTaxes ( newSalesState, newSalesTax, " " );
                        ArrayList salesStatesList = ( ArrayList ) session.getAttribute ( "salesTaxes" );

                        salesStatesList.add ( new org.apache.struts.util.LabelValueBean ( state1 + " - " + value1, state1 ) );
                        session.setAttribute ( "salesTaxes", salesStatesList );
                        subform.setSalesTaxType ( state1 );
                        value1 = null;
                    }
                }
                else
                {
                    errors.put ( "The State '" + state1 + "' already exists", null );
                    request.setAttribute ( "otherstateadded", "false" );
                    request.setAttribute ( "newsalesstate", state1 );
                    request.setAttribute ( "newsalesvalue", value1 );
                }
            }
        }
        catch ( Exception e )
        {
            errors.put ( ERROR_INTERNAL_SERVER_ERROR, ERROR_IN_SAVING_NEW_SALES_TAX_STATE );
            errors.put ( ERROR_MESSAGE_CONTACT_SYS_ADMN, null );
            session.setAttribute ( SessionKeys.ERROR_REPORT_SUBJECT, " Error: Could Not Save New Sales Tax State" );
            session.setAttribute ( SessionKeys.ERROR_REPORT, e );
        }
        request.setAttribute ( "Errors", errors );
    }

    public static ArrayList getLineItems ( HttpSession session )
    {
        ArrayList lineItems = new ArrayList ();
        for ( int i = 1; i < 6; i++ )
        {
            LineItem lineItem = new LineItem ();
            lineItem.setLineItemNo ( i );
            lineItem.setPoLineItemNo ( i );
            lineItems.add ( lineItem );
        }
        session.setAttribute ( "maxLineItemNo", "6" );

        return lineItems;
    }

    private void addNewShippingMethod ( HttpServletRequest request, HttpSession session, HashMap errors, HashMap info, QuotationForm subform, Quotation quotationFromMap )
    {
        String customerId = request.getParameter ( "customerId" );
        String method = null;
        String account = null;
        boolean checkmethodexist = true;
        int customerIdvalue = -1;

        if ( customerId != null )
        {
            session.setAttribute ( "addShippingMethodCustomerId", customerId );
        }
        else
        {
            customerId = ( String ) session.getAttribute ( "addShippingMethodCustomerId" );
        }

        String shippingMethod = subform.getShippingMethod ();
        if ( "-2".equalsIgnoreCase ( shippingMethod ) == true )
        {
            try
            {
                method = request.getParameter ( "newshippingMethod" );
                account = request.getParameter ( "newshippingAccount" );
                customerIdvalue = Integer.parseInt ( customerId );
                checkmethodexist = CustomerDAO.doesShippingMethodExist ( null, customerIdvalue, method, account );

                if ( checkmethodexist == false )
                {
                    if ( !( ( method.equals ( "" ) ) && ( account.equals ( "" ) ) ) )
                    {
                        try
                        {
                            CustomerDAO.insertShippingMethod ( null, customerIdvalue, method, account );
                            ArrayList shippingMethodList = quotationFromMap.getShippingMethods();
                            shippingMethodList.add ( new org.apache.struts.util.LabelValueBean ( method + "   #   " + account, method + "   #   " + account ) );
                            quotationFromMap.setShippingMethods( shippingMethodList );

                            subform.setNewshippingAccount ( "" );
                            subform.setNewshippingMethod ( "" );
                            String newShippingMethodLabel = null;
                            try
                            {
                                newShippingMethodLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesOrder.NewShippingMethod" );
                                if ( newShippingMethodLabel == null )
                                {
                                    newShippingMethodLabel = Labels.DEFAULT_NEW_SHIPPING_METHOD_LABEL;
                                }
                            }
                            catch ( NullPointerException e )
                            {
                                 newShippingMethodLabel = Labels.DEFAULT_NEW_SHIPPING_METHOD_LABEL;
                            }
                            info.put ( newShippingMethodLabel+" has been stored in database successfully", null );
                        }

                        catch ( ScalarException se )
                        {
                            String shippingMethodLabel = null;
                            try
                            {
                                shippingMethodLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesOrder.ShippingMethod" );
                                if ( shippingMethodLabel == null )
                                {
                                    shippingMethodLabel = Labels.DEFAULT_SHIPPING_METHOD_LABEL;
                                }
                            }
                            catch ( NullPointerException e )
                            {
                                shippingMethodLabel    = Labels.DEFAULT_SHIPPING_METHOD_LABEL;
                            }
                            errors.put ( ERROR_INTERNAL_SERVER_ERROR, "Could not Save "+shippingMethodLabel+" Value."+ method );
                            se.printStackTrace ();
                        }

                    }
                    subform.setShippingMethodName ( method + "   #   " + account );
                    subform.setShippingAccount ( method + "   #   " + account );
                    subform.setShippingMethod ( method + "   #   " + account );
                    quotationFromMap.setShippingMethod( subform.getShippingMethod() );
                }
                else
                {
                    String shippingMethodLabel = null;
                    try
                    {
                        shippingMethodLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesOrder.ShippingMethod" );
                        if ( shippingMethodLabel == null )
                        {
                            shippingMethodLabel = Labels.DEFAULT_SHIPPING_METHOD_LABEL;
                        }
                    }
                    catch ( NullPointerException e )
                    {
                        shippingMethodLabel    = Labels.DEFAULT_SHIPPING_METHOD_LABEL;
                    }
                    errors.put ( method +" "+ shippingMethodLabel+" already Exists ,Please enter another "+shippingMethodLabel+".", null );
                }

            }
            catch ( Exception e )
            {
                e.printStackTrace ();
            }
        }
    }

    public String getQuotationChangeSummary ( DBTransaction dbTransaction, Quotation newQuote, Quotation oldQuote ) throws ScalarException
    {
        boolean success = false;
        StringBuffer changeSummary = new StringBuffer ( "" );
        try
        {
            if ( oldQuote != null && newQuote != null )
            {
                if ( oldQuote.getPoRev() == null )
                {
                    oldQuote.setPoRev( "" );
                }
                if ( oldQuote.getPoDate() == null )
                {
                    oldQuote.setPoDate( "" );
                }
                if ( oldQuote.getCustomerPO () == null )
                {
                    oldQuote.setCustomerPO ( "" );
                }
                if ( oldQuote.getReason () == null )
                {
                    oldQuote.setReason ( "" );
                }
                if ( oldQuote.getContactPerson () == null )
                {
                    oldQuote.setContactPerson ( "" );
                }
                if ( oldQuote.getBillingContactPerson() == null )
                {
                    oldQuote.setBillingContactPerson ( "" );
                }
                if ( oldQuote.getShippingContactPerson () == null )
                {
                    oldQuote.setShippingContactPerson ( "" );
                }
                if ( oldQuote.getSalesPerson () == null )
                {
                    oldQuote.setSalesPerson ( "" );
                }
                if ( oldQuote.getShippingMethod () == null )
                {
                    oldQuote.setShippingMethod ( "" );
                }
                if ( oldQuote.getShippingAccount () == null )
                {
                    oldQuote.setShippingAccount ( "" );
                }
                if ( oldQuote.getDeliveryTerms () == null )
                {
                    oldQuote.setDeliveryTerms ( "" );
                }
                if ( oldQuote.getRegionalRep () == null )
                {
                    oldQuote.setRegionalRep ( "" );
                }
                if ( oldQuote.getOdm () == null )
                {
                    oldQuote.setOdm ( "" );
                }
                if ( oldQuote.getComments () == null )
                {
                    oldQuote.setComments ( "" );
                }
                if ( oldQuote.getFreightInstructions() == null )
                {
                    oldQuote.setFreightInstructions( "" );
                }
                if ( oldQuote.getShippingInstructions () == null )
                {
                    oldQuote.setShippingInstructions ( "" );
                }
                if ( oldQuote.getSalesTaxType () == null )
                {
                    oldQuote.setSalesTaxType ( "" );
                }
                if ( newQuote.getPoRev() == null )
                {
                    newQuote.setPoRev( "" );
                }
                if ( newQuote.getPoDate() == null )
                {
                    newQuote.setPoDate( "" );
                }
                if ( newQuote.getCustomerPO () == null )
                {
                    newQuote.setCustomerPO ( "" );
                }
                if ( newQuote.getReason () == null )
                {
                    newQuote.setReason ( "" );
                }
                if ( newQuote.getContactPerson () == null )
                {
                    newQuote.setContactPerson ( "" );
                }
                if ( newQuote.getBillingContactPerson() == null )
                {
                     newQuote.setBillingContactPerson ( "" );
                }
                if (  newQuote.getShippingContactPerson () == null )
                {
                     newQuote.setShippingContactPerson ( "" );
                }
                if ( newQuote.getSalesPerson () == null )
                {
                    newQuote.setSalesPerson ( "" );
                }
                if ( newQuote.getShippingMethod () == null )
                {
                    newQuote.setShippingMethod ( "" );
                }
                if ( newQuote.getShippingAccount () == null )
                {
                    newQuote.setShippingAccount ( "" );
                }
                if ( newQuote.getDeliveryTerms () == null )
                {
                    newQuote.setDeliveryTerms ( "" );
                }
                if ( newQuote.getRegionalRep () == null )
                {
                    newQuote.setRegionalRep ( "" );
                }
                if ( newQuote.getOdm () == null )
                {
                    newQuote.setOdm ( "" );
                }
                if ( newQuote.getComments () == null )
                {
                    newQuote.setComments ( "" );
                }
                if ( newQuote.getFreightInstructions() == null )
                {
                    newQuote.setFreightInstructions( "" );
                }
                if ( newQuote.getShippingInstructions () == null )
                {
                    newQuote.setShippingInstructions ( "" );
                }
                if ( newQuote.getSalesTaxType () == null )
                {
                    newQuote.setSalesTaxType ( "" );
                }
                if ( !oldQuote.getPoRev().equals ( newQuote.getPoRev() ) )
                {
                    String poRevLabel = null;
                    try
                    {
                        poRevLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesOrder.PoRevisionNumber" );
                        if ( poRevLabel == null )
                        {
                            poRevLabel = Labels.DEFAULT_SHIPPING_METHOD_LABEL;
                        }
                    }
                    catch ( NullPointerException e )
                    {
                        poRevLabel  = Labels.DEFAULT_SHIPPING_METHOD_LABEL;
                    }
                    changeSummary.append ( "<br>"+poRevLabel+" has been changed " );
                }
                if ( !oldQuote.getPoDate().equals ( newQuote.getPoDate() ) )
                {
                    String poDateLabel = null;
                    try
                    {
                        poDateLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesOrder.PoDate" );
                        if ( poDateLabel == null )
                        {
                            poDateLabel = Labels.DEFAULT_SHIPPING_METHOD_LABEL;
                        }
                    }
                    catch ( NullPointerException e )
                    {
                        poDateLabel  = Labels.DEFAULT_SHIPPING_METHOD_LABEL;
                    }
                    changeSummary.append ( "<br>"+poDateLabel+" has been changed " );
                }
                if ( !oldQuote.getCustomerPO ().equals ( newQuote.getCustomerPO () ) )
                {
                    String poNoLabel = null;
                    try
                    {
                        poNoLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesOrder.PoNumber" );
                        if ( poNoLabel == null )
                        {
                            poNoLabel = Labels.DEFAULT_SHIPPING_METHOD_LABEL;
                        }
                    }
                    catch ( NullPointerException e )
                    {
                        poNoLabel    = Labels.DEFAULT_SHIPPING_METHOD_LABEL;
                    }
                    changeSummary.append ( "<br>"+poNoLabel+" has been changed " );
                }
                if ( !oldQuote.getContactPerson ().equals ( newQuote.getContactPerson () ) )
                {
                    changeSummary.append ( "<br> Contact Person has been changed " );
                }
                if ( !oldQuote.getSalesPerson ().equals ( newQuote.getSalesPerson () ) )
                {
                    changeSummary.append ( "<br> Sales Person has been changed " );
                }
                if ( !oldQuote.getSalesRep ().equals ( newQuote.getSalesRep () ) )
                {
                    String representativeLabel = null;
                    try
                    {
                        representativeLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesRep.Name" );
                        if ( representativeLabel == null )
                        {
                            representativeLabel = Labels.DEFAULT_REPRESENTATIVE;
                        }
                    }
                    catch ( NullPointerException e )
                    {
                        representativeLabel = Labels.DEFAULT_REPRESENTATIVE;
                    }
                    changeSummary.append ( "<br> "+representativeLabel+" has been changed " );
                }

                if ( !oldQuote.getShippingMethod ().equals ( newQuote.getShippingMethod () ) )
                {
                    String shippingMethodLabel = null;
                    try
                    {
                        shippingMethodLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesOrder.ShippingMethod" );
                        if ( shippingMethodLabel == null )
                        {
                            shippingMethodLabel = Labels.DEFAULT_SHIPPING_METHOD_LABEL;
                        }
                    }
                    catch ( NullPointerException e )
                    {
                        shippingMethodLabel    = Labels.DEFAULT_SHIPPING_METHOD_LABEL;
                    }
                    changeSummary.append ( "<br> "+shippingMethodLabel+"  has been changed " );
                }
                if ( !oldQuote.getShippingAccount ().equals ( newQuote.getShippingAccount () ) )
                {
                    String shippingAccountLabel = null;
                     try
                    {
                        shippingAccountLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesOrder.ShippingAccount" );
                        if ( shippingAccountLabel == null )
                        {
                            shippingAccountLabel = Labels.DEFAULT_SHIPPING_ACCOUNT_LABEL;
                        }
                    }
                    catch ( NullPointerException e )
                    {
                        shippingAccountLabel = Labels.DEFAULT_SHIPPING_ACCOUNT_LABEL;
                    }
                    changeSummary.append ( "<br>"+shippingAccountLabel+" has been changed " );
                }
                if ( !oldQuote.getShippingTerms ().equals ( newQuote.getShippingTerms () ) )
                {
                    changeSummary.append ( "<br> Pay Terms  have been changed " );
                }

                if ( !oldQuote.getDeliveryTerms ().equals ( newQuote.getDeliveryTerms () ) )
                {
                    String deliveryTermsLabel = null;
                    try
                    {
                        deliveryTermsLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesOrder.DeliveryTerms" );
                        if ( deliveryTermsLabel == null )
                        {
                            deliveryTermsLabel = Labels.DEFAULT_DELIVERY_TERMS_LABEL;
                        }
                    }
                    catch ( NullPointerException e )
                    {
                        deliveryTermsLabel    = Labels.DEFAULT_DELIVERY_TERMS_LABEL;
                    }
                    changeSummary.append ( "<br>"+deliveryTermsLabel+"  have been changed " );
                }
                if ( !oldQuote.getRegionalRep ().equals ( newQuote.getRegionalRep () ) )
                {
                    changeSummary.append ( "<br> Regional Rep  has been changed " );
                }
                if ( oldQuote.getBookingDate ().compareTo ( newQuote.getBookingDate () ) != 0 )
                {
                    changeSummary.append ( "<br> Booking Date  has been changed " );
                }
                if ( !oldQuote.getOdm().equals ( newQuote.getOdm() ) )
                {
                    String endUserLabel = null;
                    try
                    {
                        endUserLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.endCustomer" );
                        if ( endUserLabel == null )
                        {
                            endUserLabel = Labels.DEFAULT_END_CUSTOMER_LABEL;
                        }
                    }
                    catch ( NullPointerException e )
                    {
                        endUserLabel    = Labels.DEFAULT_END_CUSTOMER_LABEL;
                    }
                    changeSummary.append ( "<br> "+endUserLabel+" has been changed " );
                }
                if ( !oldQuote.getBillingContactPerson ().equals ( newQuote.getBillingContactPerson () ) )
                {
                    changeSummary.append ( "<br> Billing Contact Person has been changed " );
                }
                if ( !oldQuote.getShippingContactPerson ().equals ( newQuote.getShippingContactPerson () ) )
                {
                    changeSummary.append ( "<br> Shipping Contact Person has been changed " );
                }
                if ( oldQuote.getShippingAddressId () != newQuote.getShippingAddressId () )
                {
                    changeSummary.append ( "<br> Shipping Address  has been changed " );
                }
                if ( oldQuote.getBillingAddressId () != newQuote.getBillingAddressId () )
                {
                    changeSummary.append ( "<br> Billing Address  has been changed " );
                }

                if ( !oldQuote.getFreightInstructions().equals ( newQuote.getFreightInstructions() ) )
                {
                    changeSummary.append ( "<br> Freight Instructions have been changed " );
                }
                if ( !oldQuote.getShippingInstructions ().equals ( newQuote.getShippingInstructions () ) )
                {
                    changeSummary.append ( "<br> Shipping Instructions have been changed " );
                }
                if ( !oldQuote.getExpiryDate().equals ( newQuote.getExpiryDate () ) )
                {
                    changeSummary.append ( "<br> Quotation Expiry Date has been changed " );
                }
                if ( !oldQuote.getComments ().equals ( newQuote.getComments () ) )
                {
                    changeSummary.append ( "<br> Comments  have been changed " );
                }
                if ( !oldQuote.getSalesTaxType ().equals ( newQuote.getSalesTaxType () ) )
                {
                    changeSummary.append ( "<br> Sales Tax type has been changed " );
                }
                if ( oldQuote.getMiscAsDouble () != newQuote.getMiscAsDouble () )
                {
                    changeSummary.append ( "<br> Misc amount has been changed " );
                }
                if ( oldQuote.getFrieghtAsDouble () != newQuote.getFrieghtAsDouble () )
                {
                    changeSummary.append ( "<br> Frieght amount has been changed " );
                }
            }
            success = true;
        }
        catch ( Exception se )
        {
            se.printStackTrace ();
            throw new ScalarException ( "Error while comparing the sales orders ", se );
        }
        return changeSummary.toString ();
    }
    private ArrayList loadShippingAddresses ( ArrayList shippingAddresses )
    {
           Address address = null;
           ArrayList shippingList = new ArrayList ();
           for ( int i = 0; i < shippingAddresses.size (); i++ )
           {
               address = ( Address ) shippingAddresses.get ( i );

               int shippingAddressId = address.getAddressId ();

               String addrStr = "";

               if ( ( address.getAddress1 () != null ) && ( address.getAddress1 ().trim ().length () != 0 ) )
               {
                   shippingList.add ( new org.apache.struts.util.LabelValueBean (
                           address.getAddress1 (), Integer.toString ( address.getAddressId () ) ) );
               }
               if ( ( address.getAddress2 () != null ) && ( address.getAddress2 ().trim ().length () != 0 ) )
               {
                   shippingList.add ( new org.apache.struts.util.LabelValueBean (
                           address.getAddress2 (), Integer.toString ( address.getAddressId () ) ) );
               }
               if ( ( address.getAddress3 () != null ) && ( address.getAddress3 ().trim ().length () != 0 ) )
               {
                   shippingList.add ( new org.apache.struts.util.LabelValueBean (
                           address.getAddress3 (), Integer.toString ( address.getAddressId () ) ) );
               }
               if ( ( address.getAddress4 () != null ) && ( address.getAddress4 ().trim ().length () != 0 ) )
               {
                   shippingList.add ( new org.apache.struts.util.LabelValueBean (
                           address.getAddress4 (), Integer.toString ( address.getAddressId () ) ) );
               }
               if ( ( address.getCity () != null ) && ( address.getCity ().trim ().length () != 0 ) )
               {
                   shippingList.add ( new org.apache.struts.util.LabelValueBean (
                           address.getCity (), Integer.toString ( address.getAddressId () ) ) );
               }

               if ( ( address.getState () != null ) && ( address.getState ().trim ().length () != 0 ) )
               {
                   addrStr = addrStr + address.getState ();
               }
               if ( ( address.getZipCode () != null ) && ( address.getZipCode ().trim ().length () != 0 ) )
               {
                   if ( ( address.getState () != null ) && ( address.getState ().trim ().length () != 0 ) )
                   {
                       addrStr = addrStr + "-";
                   }

                   addrStr = addrStr + address.getZipCode ();
               }
               if ( ( address.getCountry () != null ) && ( address.getCountry ().trim ().length () != 0 ) )
               {
                   if ( ( ( address.getZipCode () != null ) && ( address.getZipCode ().trim ().length () != 0 ) ) || ( ( address.getState () != null ) && ( address.getState ().trim ().length () != 0 ) ) )
                   {
                       addrStr = addrStr + ",";
                   }
                   addrStr = addrStr + address.getCountry ();
               }
               if ( !( "".equals ( addrStr ) ) )
               {
                   shippingList.add ( new org.apache.struts.util.LabelValueBean ( addrStr, Integer.toString ( address.getAddressId () ) ) );
               }
               shippingList.add ( new org.apache.struts.util.LabelValueBean ( "", Integer.toString ( address.getAddressId () ) ) );
           }

           //Set all the shippingAddresses in the salesOrder
           return shippingList;
       }

    public static void loadShippingAddress( Address address, ArrayList shippingList, Quotation quotationFromMap )
    {
          String addrStr = "";

           if ( ( address.getAddress1 () != null ) && ( address.getAddress1 ().trim ().length () != 0 ) )
           {
               shippingList.add ( new org.apache.struts.util.LabelValueBean (
                       address.getAddress1 (), Integer.toString ( address.getAddressId () ) ) );
           }
           if ( ( address.getAddress2 () != null ) && ( address.getAddress2 ().trim ().length () != 0 ) )
           {
               shippingList.add ( new org.apache.struts.util.LabelValueBean (
                       address.getAddress2 (), Integer.toString ( address.getAddressId () ) ) );
           }
           if ( ( address.getAddress3 () != null ) && ( address.getAddress3 ().trim ().length () != 0 ) )
           {
               shippingList.add ( new org.apache.struts.util.LabelValueBean (
                       address.getAddress3 (), Integer.toString ( address.getAddressId () ) ) );
           }
           if ( ( address.getAddress4 () != null ) && ( address.getAddress4 ().trim ().length () != 0 ) )
           {
               shippingList.add ( new org.apache.struts.util.LabelValueBean (
                       address.getAddress4 (), Integer.toString ( address.getAddressId () ) ) );
           }
           if ( ( address.getCity () != null ) && ( address.getCity ().trim ().length () != 0 ) )
           {
               shippingList.add ( new org.apache.struts.util.LabelValueBean (
                       address.getCity (), Integer.toString ( address.getAddressId () ) ) );
           }

           if ( ( address.getState () != null ) && ( address.getState ().trim ().length () != 0 ) )
           {
               addrStr = addrStr + address.getState ();
           }
           if ( ( address.getZipCode () != null ) && ( address.getZipCode ().trim ().length () != 0 ) )
           {
               if ( ( address.getState () != null ) && ( address.getState ().trim ().length () != 0 ) )
               {
                   addrStr = addrStr + "-";
               }
               addrStr = addrStr + address.getZipCode ();
           }
           if ( ( address.getCountry () != null ) && ( address.getCountry ().trim ().length () != 0 ) )
           {
               if ( ( ( address.getZipCode () != null ) && ( address.getZipCode ().trim ().length () != 0 ) ) || ( ( address.getState () != null ) && ( address.getState ().trim ().length () != 0 ) ) )
               {
                   addrStr = addrStr + ",";
               }
               addrStr = addrStr + address.getCountry ();
           }
           if ( !( "".equals ( addrStr ) ) )
           {
               shippingList.add ( new org.apache.struts.util.LabelValueBean ( addrStr, Integer.toString ( address.getAddressId () ) ) );
           }
           shippingList.add ( new org.apache.struts.util.LabelValueBean ( "", Integer.toString ( address.getAddressId () ) ) );
           ArrayList shippingContactPersons = new ArrayList();
           shippingContactPersons.add( address.getContactPerson() );
           quotationFromMap.setContactPersonsForShippingAddress( shippingContactPersons );
   }

}